Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,46 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from duckduckgo_search import DDGS
|
| 4 |
-
from
|
| 5 |
-
from langchain.chains import RetrievalQA
|
| 6 |
|
| 7 |
-
# 🔱
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
-
def inachi_ai_response(user_input):
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
return f"Specialist, අන්තර්ජාලයෙන් ලැබුණු තොරතුරු අනුව:\n{context}\n\n---"
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
demo
|
| 36 |
-
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from duckduckgo_search import DDGS
|
| 4 |
+
from huggingface_hub import InferenceClient
|
|
|
|
| 5 |
|
| 6 |
+
# 🔱 Hugging Face Token එක Secret එකෙන් ලබා ගැනීම
|
| 7 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
+
|
| 9 |
+
# 🔱 Gemma 2 සඳහා Client එක සකස් කිරීම
|
| 10 |
+
client = InferenceClient("google/gemma-2-9b-it", token=HF_TOKEN)
|
| 11 |
|
| 12 |
+
def web_search(query):
|
|
|
|
| 13 |
try:
|
| 14 |
+
with DDGS() as ddgs:
|
| 15 |
+
results = [r['body'] for r in ddgs.text(query, max_results=3)]
|
| 16 |
+
return "\n".join(results)
|
| 17 |
+
except Exception:
|
| 18 |
+
return "තොරතුරු සෙවීමේදී බාධාවක් ඇති විය."
|
| 19 |
+
|
| 20 |
+
def inachi_ai_response(user_input, history):
|
| 21 |
+
# 1. Web Search කිරීම
|
| 22 |
+
context = web_search(user_input)
|
| 23 |
+
|
| 24 |
+
# 2. Prompt එක සැකසීම
|
| 25 |
+
prompt = f"Context: {context}\n\nQuestion: {user_input}\n\nAnswer like a Specialist AI:"
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# 3. Gemma 2 වෙතින් පිළිතුර ලබා ගැනීම
|
| 28 |
+
response = ""
|
| 29 |
+
for message in client.chat_completion(
|
| 30 |
+
messages=[{"role": "user", "content": prompt}],
|
| 31 |
+
max_tokens=1024,
|
| 32 |
+
stream=True,
|
| 33 |
+
):
|
| 34 |
+
token = message.choices[0].delta.content
|
| 35 |
+
response += token
|
| 36 |
+
yield response
|
| 37 |
+
|
| 38 |
+
# 🔱 Gradio ChatInterface එක (මේක දැන් ගොඩක් ලස්සනයි)
|
| 39 |
+
demo = gr.ChatInterface(
|
| 40 |
+
fn=inachi_ai_response,
|
| 41 |
+
title="INACHI-PRIME (Gemma 2 Web-Search)",
|
| 42 |
+
description="Specialist, මම Gemma 2 සහ සජීවී අන්තර්ජාල සෙවුම් බලයෙන් ක්රියාත්මක වෙමි."
|
| 43 |
+
)
|
| 44 |
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
demo.launch()
|
|
|