pradeepsengarr commited on
Commit
3f28fb7
Β·
verified Β·
1 Parent(s): 03245d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -58
app.py CHANGED
@@ -1,60 +1,61 @@
1
  import gradio as gr
2
- import faiss
3
- import torch
4
- from sentence_transformers import SentenceTransformer
5
- from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModelForSeq2SeqLM
6
-
7
- # ---------- Load models ----------
8
- embed_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
9
-
10
- gen_tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2")
11
- gen_model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2", torch_dtype=torch.float32)
12
-
13
- # Example: EN->HI
14
- trans_tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-hi")
15
- trans_model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-hi")
16
-
17
- # ---------- Sample docs + FAISS index ----------
18
- documents = [
19
- "The Taj Mahal is an ivory-white marble mausoleum in India.",
20
- "ChatGPT is a large language model developed by OpenAI.",
21
- "RAG combines retrieval-based and generation-based approaches."
22
- ]
23
-
24
- doc_embeddings = embed_model.encode(documents, convert_to_tensor=True)
25
- index = faiss.IndexFlatL2(doc_embeddings.shape[1])
26
- index.add(doc_embeddings.cpu().numpy())
27
-
28
- # ---------- RAG Function ----------
29
- def rag_translate(query, target_lang='hi'):
30
- query_vec = embed_model.encode([query])
31
- _, top_indices = index.search(query_vec, k=1)
32
- retrieved_doc = documents[top_indices[0][0]]
33
-
34
- prompt = f"Context: {retrieved_doc}\nQuestion: {query}\nAnswer:"
35
- inputs = gen_tokenizer(prompt, return_tensors="pt")
36
- outputs = gen_model.generate(**inputs, max_new_tokens=64)
37
- answer_en = gen_tokenizer.decode(outputs[0], skip_special_tokens=True)
38
-
39
- # Translate if requested
40
- if target_lang and target_lang != 'en':
41
- trans_inputs = trans_tokenizer(answer_en, return_tensors="pt", truncation=True)
42
- trans_output = trans_model.generate(**trans_inputs)
43
- translated = trans_tokenizer.decode(trans_output[0], skip_special_tokens=True)
44
- return f"πŸ” Answer:\n{answer_en}\n\n🌐 Translated:\n{translated}"
 
 
 
 
 
 
 
45
 
46
- return f"πŸ” Answer:\n{answer_en}"
47
-
48
- # ---------- Gradio UI ----------
49
- iface = gr.Interface(
50
- fn=rag_translate,
51
- inputs=[
52
- gr.Textbox(label="Ask a Question"),
53
- gr.Dropdown(choices=["en", "hi", "fr", "es"], value="hi", label="Target Language")
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()