Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,61 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 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 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
outputs=gr.Textbox(label="Answer"),
|
| 56 |
-
title="π§ RAG + π Translator",
|
| 57 |
-
description="A lightweight RAG system with answer translation. Powered by Phi-2 + MiniLM + Opus MT."
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
TOGETHER_API_KEY = "tgp_v1_ZytvDbMu9PMwIlnBZEfYSq9nzJAYwS0MecjY9Kt7RxE"
|
| 5 |
+
SERPER_API_KEY = "75f06519187851ad63486c3012b34c5e0e6501f1"
|
| 6 |
+
|
| 7 |
+
# Step 1: Use Serper to search the web
|
| 8 |
+
def web_search(query):
|
| 9 |
+
url = "https://google.serper.dev/search"
|
| 10 |
+
headers = {"X-API-KEY": SERPER_API_KEY}
|
| 11 |
+
payload = {"q": query}
|
| 12 |
+
res = requests.post(url, json=payload, headers=headers)
|
| 13 |
+
results = res.json().get("organic", [])
|
| 14 |
+
if not results:
|
| 15 |
+
return "No relevant search results found."
|
| 16 |
+
context = "\n".join([f"{item['title']}: {item['link']}\n{item['snippet']}" for item in results[:3]])
|
| 17 |
+
return context
|
| 18 |
+
|
| 19 |
+
# Step 2: Use Together API to get a response
|
| 20 |
+
def call_together_llm(context, question):
|
| 21 |
+
url = "https://api.together.xyz/v1/chat/completions"
|
| 22 |
+
headers = {
|
| 23 |
+
"Authorization": f"Bearer {TOGETHER_API_KEY}",
|
| 24 |
+
"Content-Type": "application/json"
|
| 25 |
+
}
|
| 26 |
+
messages = [
|
| 27 |
+
{"role": "system", "content": "You are a helpful assistant that uses the web context to answer."},
|
| 28 |
+
{"role": "user", "content": f"Context: {context}\n\nQuestion: {question}"}
|
| 29 |
+
]
|
| 30 |
+
data = {
|
| 31 |
+
"model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 32 |
+
"messages": messages,
|
| 33 |
+
"temperature": 0.7,
|
| 34 |
+
"max_tokens": 512
|
| 35 |
+
}
|
| 36 |
+
res = requests.post(url, headers=headers, json=data)
|
| 37 |
+
return res.json()['choices'][0]['message']['content']
|
| 38 |
+
|
| 39 |
+
# Step 3: Combine Search + LLM for RAG
|
| 40 |
+
def rag_chatbot(question):
|
| 41 |
+
try:
|
| 42 |
+
context = web_search(question)
|
| 43 |
+
response = call_together_llm(context, question)
|
| 44 |
+
return response
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"β Error: {str(e)}"
|
| 47 |
+
|
| 48 |
+
# Step 4: UI with Gradio
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("# π€ Free RAG Chatbot with Together + Serper")
|
| 51 |
+
gr.Markdown("Ask any question. It will search the web and give a smart answer.")
|
| 52 |
|
| 53 |
+
with gr.Row():
|
| 54 |
+
input_box = gr.Textbox(placeholder="Ask a question...", label="Your Question")
|
| 55 |
+
output_box = gr.Textbox(label="Answer", lines=10)
|
| 56 |
+
|
| 57 |
+
submit_btn = gr.Button("π Search & Answer")
|
| 58 |
+
|
| 59 |
+
submit_btn.click(fn=rag_chatbot, inputs=input_box, outputs=output_box)
|
| 60 |
+
|
| 61 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|