Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,19 @@ 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 |
-
# 🔱
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def web_search(query):
|
| 13 |
try:
|
|
@@ -23,31 +31,22 @@ def web_search(query):
|
|
| 23 |
return "Web search unavailable."
|
| 24 |
|
| 25 |
def inachi_ai_response(user_input, history):
|
| 26 |
-
# 1. Web Search කිරීම
|
| 27 |
context = web_search(user_input)
|
| 28 |
|
| 29 |
-
#
|
| 30 |
messages = [
|
| 31 |
-
{
|
| 32 |
-
|
| 33 |
-
"content": "You are INACHI-AI, a professional specialist assistant. Use the provided web context to answer precisely."
|
| 34 |
-
},
|
| 35 |
-
{
|
| 36 |
-
"role": "user",
|
| 37 |
-
"content": f"Web Context: {context}\n\nQuestion: {user_input}"
|
| 38 |
-
}
|
| 39 |
]
|
| 40 |
|
| 41 |
-
# 3. Chat Completion පාවිච්චි කිරීම (Providers ලා බලාපොරොත්තු වන ක්රමය)
|
| 42 |
response = ""
|
| 43 |
try:
|
| 44 |
-
#
|
| 45 |
for message in client.chat_completion(
|
| 46 |
-
model="mistralai/Mistral-7B-Instruct-v0.3",
|
| 47 |
messages=messages,
|
| 48 |
max_tokens=1024,
|
| 49 |
stream=True
|
| 50 |
-
):
|
| 51 |
token = message.choices[0].delta.content
|
| 52 |
if token:
|
| 53 |
response += token
|
|
@@ -55,11 +54,10 @@ def inachi_ai_response(user_input, history):
|
|
| 55 |
except Exception as e:
|
| 56 |
yield f"Error Specialist: {str(e)}"
|
| 57 |
|
| 58 |
-
# 🔱 Gradio ChatInterface
|
| 59 |
demo = gr.ChatInterface(
|
| 60 |
fn=inachi_ai_response,
|
| 61 |
title="INACHI-CORE (Specialist Engine)",
|
| 62 |
-
description="Specialist
|
| 63 |
)
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
|
|
|
| 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 කෙනෙක්ව තෝරා ගැනීම (උදා: Together AI හෝ Novita)
|
| 10 |
+
# Hugging Face එකේ Model URL එක අවසානයට Provider danna puluwan.
|
| 11 |
+
# Format: "model_id", provider="provider_name"
|
| 12 |
+
client = InferenceClient(
|
| 13 |
+
model="mistralai/Mistral-7B-Instruct-v0.3",
|
| 14 |
+
token=HF_TOKEN,
|
| 15 |
+
# මෙතනට ඔයා කැමති Provider කෙනෙක් දාන්න පුළුවන් (e.g., "novita", "together")
|
| 16 |
+
# හැබැයි Serverless API එකේදී Hugging Face විසින්ම මේක පාලනය කරන නිසා
|
| 17 |
+
# පහත ක්රමය වඩාත් ස්ථාවරයි.
|
| 18 |
+
)
|
| 19 |
|
| 20 |
def web_search(query):
|
| 21 |
try:
|
|
|
|
| 31 |
return "Web search unavailable."
|
| 32 |
|
| 33 |
def inachi_ai_response(user_input, history):
|
|
|
|
| 34 |
context = web_search(user_input)
|
| 35 |
|
| 36 |
+
# 🔱 Inachi-Style System Instructions (ඔයාගේ VOID-X එකේ වගේ)
|
| 37 |
messages = [
|
| 38 |
+
{"role": "system", "content": "You are INACHI-AI, a professional assistant. Use web context to help the user."},
|
| 39 |
+
{"role": "user", "content": f"Context: {context}\n\nQuestion: {user_input}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
]
|
| 41 |
|
|
|
|
| 42 |
response = ""
|
| 43 |
try:
|
| 44 |
+
# Chat Completion එකේදී සෘජුවම Model එකට අදාළ Provider router එක ක්රියාත්මක වේ
|
| 45 |
for message in client.chat_completion(
|
|
|
|
| 46 |
messages=messages,
|
| 47 |
max_tokens=1024,
|
| 48 |
stream=True
|
| 49 |
+
) :
|
| 50 |
token = message.choices[0].delta.content
|
| 51 |
if token:
|
| 52 |
response += token
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
yield f"Error Specialist: {str(e)}"
|
| 56 |
|
|
|
|
| 57 |
demo = gr.ChatInterface(
|
| 58 |
fn=inachi_ai_response,
|
| 59 |
title="INACHI-CORE (Specialist Engine)",
|
| 60 |
+
description="Provider-Aware Specialist Assistant"
|
| 61 |
)
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|