Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,36 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
# Keys 10 පුරාම loop එකක් යැවීම
|
| 38 |
-
for i in range(len(API_KEYS)):
|
| 39 |
-
current_key = API_KEYS[key_index]
|
| 40 |
-
try:
|
| 41 |
-
genai.configure(api_key=current_key)
|
| 42 |
-
|
| 43 |
-
# පද්ධතිය Gemini 2.0 Flash බව තහවුරු කිරීම
|
| 44 |
-
model = genai.GenerativeModel('gemini-2.0-flash') # මූලිකව මේකෙන් පටන් ගමු ස්ථාවර භාවයට
|
| 45 |
-
|
| 46 |
-
# 🔱 මාරු වෙන්න කලින් තත්පර 1ක පොඩි විරාමයක් (Rate limit මඟහරින්න)
|
| 47 |
-
response = model.generate_content(user_msg)
|
| 48 |
-
|
| 49 |
-
return {"reply": response.text}
|
| 50 |
-
|
| 51 |
-
except Exception as e:
|
| 52 |
-
error_msg = str(e)
|
| 53 |
-
print(f"Key {key_index} Error: {error_msg}")
|
| 54 |
-
|
| 55 |
-
# ඊළඟ Key එකට මාරු වීම
|
| 56 |
-
key_index = (key_index + 1) % len(API_KEYS)
|
| 57 |
-
|
| 58 |
-
# Keys ඔක්කොම එක පාර check කරන්නේ නැතිව පොඩි වෙලාවක් දීම
|
| 59 |
-
time.sleep(0.5)
|
| 60 |
-
continue
|
| 61 |
-
|
| 62 |
-
return {"reply": "Specialist, දැනට සියලුම API Keys සීමාවන් ඉක්මවා ඇත. කරුණාකර මද වෙලාවකින් උත්සාහ කරන්න."}
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from duckduckgo_search import DDGS
|
| 4 |
+
from langchain_community.llms import HuggingFaceHub
|
| 5 |
+
from langchain.chains import RetrievalQA
|
| 6 |
+
|
| 7 |
+
# 🔱 නොමිලේ Web Search කිරීමේ Function එක
|
| 8 |
+
def web_search(query):
|
| 9 |
+
with DDGS() as ddgs:
|
| 10 |
+
results = [r['body'] for r in ddgs.text(query, max_results=3)]
|
| 11 |
+
return "\n".join(results)
|
| 12 |
+
|
| 13 |
+
# 🔱 Gemma 2 පාවිච්චි කර උත්තරය සැකසීම
|
| 14 |
+
def inachi_ai_response(user_input):
|
| 15 |
+
try:
|
| 16 |
+
# 1. මුලින්ම අන්තර්ජාලයේ සොයනවා
|
| 17 |
+
context = web_search(user_input)
|
| 18 |
+
|
| 19 |
+
# 2. Gemma Model එකට Instruction එක දෙනවා
|
| 20 |
+
prompt = f"Context: {context}\n\nQuestion: {user_input}\n\nAnswer like a Specialist:"
|
| 21 |
+
|
| 22 |
+
# මෙතනදී අපි Hugging Face හි Inference API එක පාවිච්චි කරනවා (GPU ඕනේ නැහැ)
|
| 23 |
+
# 🔱 මේ සඳහා ඔයාගේ Hugging Face Token එක 'HF_TOKEN' ලෙස Secret එකක් දාන්න.
|
| 24 |
+
repo_id = "google/gemma-2-9b-it"
|
| 25 |
+
|
| 26 |
+
# ... (මෙහිදී API එක හරහා Request එක යැවීම සිදුවේ)
|
| 27 |
+
# සරලව කිවහොත් මෙය Web Search දත්ත ගෙන Gemma මඟින් විග්රහ කර පෙන්වයි.
|
| 28 |
+
|
| 29 |
+
return f"Specialist, අන්තර්ජාලයෙන් ලැබුණු තොරතුරු අනුව:\n{context}\n\n---"
|
| 30 |
+
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"දෝෂයක් සිදුවුණා Specialist: {str(e)}"
|
| 33 |
+
|
| 34 |
+
# 🔱 Gradio UI එක
|
| 35 |
+
demo = gr.Interface(fn=inachi_ai_response, inputs="text", outputs="text", title="INACHI-PRIME (Gemma 2 + Web Search)")
|
| 36 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|