Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -16,10 +16,9 @@ def get_text_from_url(url):
|
|
16 |
soup = BeautifulSoup(response.text, 'html.parser')
|
17 |
texts = soup.find_all(text=True)
|
18 |
visible_texts = filter(tag_visible, texts)
|
19 |
-
|
20 |
-
return "\n".join(t.strip() for t in visible_texts if t.strip())
|
21 |
|
22 |
-
#
|
23 |
text_list = []
|
24 |
homepage_url = "https://sites.google.com/view/abhilashnandy/home/"
|
25 |
extensions = ["", "pmrf-profile-page"]
|
@@ -27,51 +26,53 @@ for ext in extensions:
|
|
27 |
url_text = get_text_from_url(homepage_url + ext)
|
28 |
text_list.append(url_text)
|
29 |
|
30 |
-
#
|
|
|
|
|
31 |
SYSTEM_MESSAGE = (
|
32 |
"You are a QA chatbot to answer queries (in less than 30 words) on my homepage that has the following information -\n\n"
|
33 |
+ "\n\n".join(text_list)
|
34 |
+ "\n\n"
|
35 |
)
|
36 |
|
37 |
-
# Use a
|
38 |
-
|
|
|
39 |
|
40 |
def respond(message, history: list[tuple[str, str]], system_message=SYSTEM_MESSAGE,
|
41 |
max_tokens=140, temperature=0.7, top_p=0.95):
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
try:
|
50 |
-
response = client.
|
51 |
-
|
52 |
-
|
53 |
temperature=temperature,
|
54 |
top_p=top_p,
|
|
|
55 |
)
|
56 |
-
|
57 |
-
generated_text = response[0]["generated_text"]
|
58 |
-
# Attempt to extract the answer by splitting at "Answer:"
|
59 |
-
answer = generated_text.split("Answer:")[-1].strip().split("\n")[0].strip()
|
60 |
-
return answer
|
61 |
except Exception as e:
|
62 |
print(f"An error occurred: {e}")
|
63 |
return str(e)
|
64 |
|
|
|
65 |
markdown_note = "## Ask Anything About Me! (Might show a tad bit of hallucination!)"
|
66 |
|
67 |
demo = gr.Blocks()
|
68 |
-
|
69 |
with demo:
|
70 |
gr.Markdown(markdown_note)
|
71 |
gr.ChatInterface(
|
72 |
-
respond,
|
73 |
examples=["Yo who dis Abhilash?", "What is Abhilash's most recent publication?"],
|
74 |
-
additional_inputs=[
|
|
|
|
|
75 |
)
|
76 |
|
77 |
if __name__ == "__main__":
|
|
|
16 |
soup = BeautifulSoup(response.text, 'html.parser')
|
17 |
texts = soup.find_all(text=True)
|
18 |
visible_texts = filter(tag_visible, texts)
|
19 |
+
return "\n".join(t.strip() for t in visible_texts)
|
|
|
20 |
|
21 |
+
# Get the text from your homepage (and any additional extensions as needed)
|
22 |
text_list = []
|
23 |
homepage_url = "https://sites.google.com/view/abhilashnandy/home/"
|
24 |
extensions = ["", "pmrf-profile-page"]
|
|
|
26 |
url_text = get_text_from_url(homepage_url + ext)
|
27 |
text_list.append(url_text)
|
28 |
|
29 |
+
# Optionally, repeat for sub-links if necessary
|
30 |
+
|
31 |
+
# Build a system message with the homepage info
|
32 |
SYSTEM_MESSAGE = (
|
33 |
"You are a QA chatbot to answer queries (in less than 30 words) on my homepage that has the following information -\n\n"
|
34 |
+ "\n\n".join(text_list)
|
35 |
+ "\n\n"
|
36 |
)
|
37 |
|
38 |
+
# Use a model that works well on CPU, has a decently long context, and low inference latency.
|
39 |
+
# Here we choose a small chat-optimized model:
|
40 |
+
client = InferenceClient("TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF")
|
41 |
|
42 |
def respond(message, history: list[tuple[str, str]], system_message=SYSTEM_MESSAGE,
|
43 |
max_tokens=140, temperature=0.7, top_p=0.95):
|
44 |
+
messages = [{"role": "system", "content": system_message}]
|
45 |
+
for val in history:
|
46 |
+
if len(val) >= 1:
|
47 |
+
messages.append({"role": "user", "content": "Question: " + val[0]})
|
48 |
+
if len(val) >= 2:
|
49 |
+
messages.append({"role": "assistant", "content": "Answer: " + val[1]})
|
50 |
+
messages.append({"role": "user", "content": message})
|
51 |
try:
|
52 |
+
response = client.chat_completion(
|
53 |
+
messages,
|
54 |
+
max_tokens=max_tokens,
|
55 |
temperature=temperature,
|
56 |
top_p=top_p,
|
57 |
+
# stream=True, # Uncomment to enable streaming
|
58 |
)
|
59 |
+
return response.choices[0].message["content"]
|
|
|
|
|
|
|
|
|
60 |
except Exception as e:
|
61 |
print(f"An error occurred: {e}")
|
62 |
return str(e)
|
63 |
|
64 |
+
initial_message = [("user", "Yo who dis Abhilash?")]
|
65 |
markdown_note = "## Ask Anything About Me! (Might show a tad bit of hallucination!)"
|
66 |
|
67 |
demo = gr.Blocks()
|
|
|
68 |
with demo:
|
69 |
gr.Markdown(markdown_note)
|
70 |
gr.ChatInterface(
|
71 |
+
fn=respond,
|
72 |
examples=["Yo who dis Abhilash?", "What is Abhilash's most recent publication?"],
|
73 |
+
additional_inputs=[
|
74 |
+
# You can add extra Gradio components here if needed.
|
75 |
+
],
|
76 |
)
|
77 |
|
78 |
if __name__ == "__main__":
|