minzo456 commited on
Commit
a444fc3
·
verified ·
1 Parent(s): 0d864db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -51
app.py CHANGED
@@ -1,62 +1,47 @@
1
- import os
2
  import gradio as gr
3
- from duckduckgo_search import DDGS
4
- from huggingface_hub import InferenceClient
5
 
6
- # 🔱 HF Token එක ලබා ගැනීම
7
- HF_TOKEN = os.getenv("HF_TOKEN")
 
8
 
9
- # 🔱 Provider කෙනෙක්ව සෘජුවම තෝරා ගැනීම
10
- # මෙහිදී 'provider=' කොටස මඟින් සෘජුවම අදාළ Server එකට Request එක යොමු කරයි.
11
- client = InferenceClient(
12
- token=HF_TOKEN
 
 
13
  )
14
 
15
- def web_search(query):
16
- try:
17
- with DDGS() as ddgs:
18
- results = ddgs.text(query, max_results=3)
19
- search_data = ""
20
- if results:
21
- for r in results:
22
- search_data += f"\nSource: {r['title']}\nContent: {r['body']}\n"
23
- return search_data
24
- return "No web results found."
25
- except Exception:
26
- return "Web search unavailable."
27
-
28
- def inachi_ai_response(user_input, history):
29
- context = web_search(user_input)
30
 
31
- messages = [
32
- {"role": "system", "content": "You are INACHI-AI, a professional assistant. Use web context to help the user."},
33
- {"role": "user", "content": f"Context: {context}\n\nQuestion: {user_input}"}
34
- ]
35
 
36
- response = ""
37
- try:
38
- # 🔱 මෙතනදී තමයි Model එක සහ Provider එක සෘජුවම සම්බන්ධ කරන්නේ
39
- # උදා: Novita පාවිච්චි කිරීමට 'provider="novita"' ලෙස එක් කරන්න
40
- for message in client.chat_completion(
41
- model="google/gemma-4-E2B",
42
- messages=messages,
43
- max_tokens=1024,
44
- stream=True,
45
- # provider="novita" # <--- ඔන්න මෙතනට Provider දාන්න පුළුවන්
46
- ):
47
- token = message.choices[0].delta.content
48
- if token:
49
- response += token
50
- yield response
51
- except Exception as e:
52
- yield f"Error Specialist: {str(e)}"
53
 
54
- # 🔱 Gradio Interface
55
- demo = gr.ChatInterface(
56
- fn=inachi_ai_response,
57
- title="INACHI-CORE (Provider-Locked Edition)",
58
- description="Specialist, මම දැන් සෘජු Provider සම්බන්ධතාවයක් හරහා ක්‍රියාත්මක වෙමි."
59
- )
 
 
 
 
60
 
61
  if __name__ == "__main__":
62
  demo.launch()
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import pipeline
4
 
5
+ # Model configuration
6
+ # Note: Use "google/gemma-2-2b-it" as it is highly efficient for Free Tier Spaces
7
+ model_id = "google/gemma-2-2b-it"
8
 
9
+ # Specialist Inference Engine initialization
10
+ pipe = pipeline(
11
+ "text-generation",
12
+ model=model_id,
13
+ model_kwargs={"torch_dtype": torch.bfloat16},
14
+ device_map="auto",
15
  )
16
 
17
+ def specialist_respond(message, history):
18
+ # System prompt to maintain identity
19
+ system_prompt = "You are a highly advanced AI assistant developed under the INACHI AI project. Be precise and technical."
20
+
21
+ # Building the conversation format for Gemma
22
+ messages = [{"role": "system", "content": system_prompt}]
23
+ for val in history:
24
+ if val[0]: messages.append({"role": "user", "content": val[0]})
25
+ if val[1]: messages.append({"role": "assistant", "content": val[1]})
26
+
27
+ messages.append({"role": "user", "content": message})
 
 
 
 
28
 
29
+ # Generating the response
30
+ prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
31
+ outputs = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
 
32
 
33
+ return outputs[0]["generated_text"][len(prompt):]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
+ # 🔱 INACHI AI UI Design
36
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
37
+ gr.Markdown("""# 🔱 INACHI AI: SPECIALIST INTERFACE
38
+ ### Powered by Gemma 2B | Authorized Access: MINZO-PRIME""")
39
+
40
+ chatbot = gr.ChatInterface(
41
+ fn=specialist_respond,
42
+ title="INACHI-GEMMA V1",
43
+ description="Advanced neural processing unit for system research and development.",
44
+ )
45
 
46
  if __name__ == "__main__":
47
  demo.launch()