Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -32,46 +32,29 @@ SYSTEM_MESSAGE = (
|
|
32 |
"Context: " + " ".join(text_list)
|
33 |
)
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
output += part
|
59 |
-
return output.strip()
|
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 |
-
)
|
75 |
|
76 |
-
if __name__ ==
|
77 |
-
|
|
|
32 |
"Context: " + " ".join(text_list)
|
33 |
)
|
34 |
|
35 |
+
# Create a Hugging Face Inference client using a CPU-friendly model.
|
36 |
+
# Here we use 'google/flan-t5-base' as an example; you can adjust the model if needed.
|
37 |
+
client = InferenceClient(model="google/flan-t5-base")
|
38 |
+
|
39 |
+
def answer_query(query):
|
40 |
+
# Compose a prompt using the system message, user query, and a reminder for a short answer.
|
41 |
+
prompt = SYSTEM_MESSAGE + "\nUser: " + query + "\nAnswer in less than 30 words:"
|
42 |
+
# Generate answer with a limit on new tokens to ensure brevity.
|
43 |
+
result = client.text_generation(prompt, max_new_tokens=60)
|
44 |
+
# Handle both list or direct string responses from the inference client.
|
45 |
+
if isinstance(result, list):
|
46 |
+
answer = result[0].get("generated_text", "")
|
47 |
+
else:
|
48 |
+
answer = result
|
49 |
+
return answer.strip()
|
50 |
+
|
51 |
+
iface = gr.Interface(
|
52 |
+
fn=answer_query,
|
53 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your question here..."),
|
54 |
+
outputs="text",
|
55 |
+
title="Homepage QA Chatbot",
|
56 |
+
description="A chatbot answering queries about the homepage using pre-fetched context."
|
57 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
if __name__ == '__main__':
|
60 |
+
iface.launch()
|