Arkm20 commited on
Commit
d4ab09c
·
verified ·
1 Parent(s): bdf54a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py CHANGED
@@ -52,3 +52,43 @@ async def search(q: Query):
52
  return {"answer": response.choices[0].message.content}
53
  except Exception as e:
54
  return {"answer": f"Error: {str(e)}"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  return {"answer": response.choices[0].message.content}
53
  except Exception as e:
54
  return {"answer": f"Error: {str(e)}"}
55
+
56
+
57
+ def search(prompt):
58
+ results = []
59
+ with DDGS() as ddgs:
60
+ for r in ddgs.text(prompt, max_results=3):
61
+ if r.get("href"):
62
+ results.append(r["href"])
63
+ if len(results) == 3:
64
+ break
65
+
66
+ data = {}
67
+ headers = {"User-Agent": "Mozilla/5.0"}
68
+ for url in results:
69
+ try:
70
+ res = requests.get(url, headers=headers, timeout=10)
71
+ soup = BeautifulSoup(res.content, "html.parser")
72
+ text = soup.get_text(separator=' ', strip=True)
73
+ data[url] = text[:5000]
74
+ except Exception as e:
75
+ data[url] = f"Error retrieving content: {e}"
76
+ return data
77
+
78
+ @app.get("/ask")
79
+ def ask(request: Request):
80
+ query_param = request.query_params.get("prompt")
81
+ if not query_param:
82
+ return {"error": "No prompt provided"}
83
+
84
+ sources = search(query_param)
85
+
86
+ content_blocks = "\n\n".join([f"Source: {url}\n{text}" for url, text in sources.items()])
87
+ combined_prompt = f"{content_blocks}\n\nUser: {query_param}"
88
+
89
+ response = client.chat.completions.create(
90
+ model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
91
+ messages=[{"role": "user", "content": combined_prompt}]
92
+ )
93
+
94
+ return {"answer": response.choices[0].message.content}