Update app.py
Browse files
app.py
CHANGED
|
@@ -3,12 +3,12 @@ from langchain_huggingface import HuggingFacePipeline
|
|
| 3 |
from langchain.chains import ConversationChain
|
| 4 |
from langchain.memory import ConversationBufferMemory
|
| 5 |
|
| 6 |
-
# Load lightweight model (distilgpt2, ~82M parameters, fast on CPU)
|
| 7 |
llm = HuggingFacePipeline.from_model_id(
|
| 8 |
model_id="distilgpt2",
|
| 9 |
task="text-generation",
|
| 10 |
pipeline_kwargs={
|
| 11 |
-
"max_new_tokens":
|
| 12 |
"do_sample": True,
|
| 13 |
"temperature": 0.7, # Balanced creativity
|
| 14 |
"top_k": 40,
|
|
@@ -30,23 +30,26 @@ conversation = ConversationChain(
|
|
| 30 |
def chat_with_agent(message, history):
|
| 31 |
try:
|
| 32 |
response = conversation.predict(input=message)
|
| 33 |
-
# Clean up response
|
| 34 |
-
response = response.strip().split("\n")[0]
|
| 35 |
-
return response
|
| 36 |
except Exception as e:
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
# Gradio chat interface
|
| 40 |
iface = gr.ChatInterface(
|
| 41 |
fn=chat_with_agent,
|
| 42 |
-
title="
|
| 43 |
-
description="
|
| 44 |
-
examples=[
|
| 45 |
-
"My name is Alex. What's my name?",
|
| 46 |
-
"Tell me a quick joke."
|
| 47 |
-
]
|
| 48 |
)
|
| 49 |
|
| 50 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
if __name__ == "__main__":
|
| 52 |
iface.launch()
|
|
|
|
| 3 |
from langchain.chains import ConversationChain
|
| 4 |
from langchain.memory import ConversationBufferMemory
|
| 5 |
|
| 6 |
+
# Load a very lightweight model (distilgpt2, ~82M parameters, fast on CPU)
|
| 7 |
llm = HuggingFacePipeline.from_model_id(
|
| 8 |
model_id="distilgpt2",
|
| 9 |
task="text-generation",
|
| 10 |
pipeline_kwargs={
|
| 11 |
+
"max_new_tokens": 100, # Short responses for speed
|
| 12 |
"do_sample": True,
|
| 13 |
"temperature": 0.7, # Balanced creativity
|
| 14 |
"top_k": 40,
|
|
|
|
| 30 |
def chat_with_agent(message, history):
|
| 31 |
try:
|
| 32 |
response = conversation.predict(input=message)
|
| 33 |
+
# Clean up response (distilgpt2 can be verbose)
|
| 34 |
+
response = response.strip().split("\n")[0]
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
+
response = f"Error: {str(e)}. Try rephrasing your question."
|
| 37 |
+
return response
|
| 38 |
|
| 39 |
# Gradio chat interface
|
| 40 |
iface = gr.ChatInterface(
|
| 41 |
fn=chat_with_agent,
|
| 42 |
+
title="Fast Free AI Agent",
|
| 43 |
+
description="A lightweight conversational AI that remembers our talks. Hosted free on Hugging Face Spaces. Responses in ~3-10 seconds.",
|
| 44 |
+
examples=["My name is Alex. What's my name?", "Tell me a short joke."]
|
|
|
|
|
|
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
+
# Add a clear memory button
|
| 48 |
+
def clear_memory():
|
| 49 |
+
memory.clear()
|
| 50 |
+
return "Conversation history cleared!"
|
| 51 |
+
|
| 52 |
+
iface.additional_inputs = [gr.Button("Clear Memory", onclick=clear_memory)]
|
| 53 |
+
|
| 54 |
if __name__ == "__main__":
|
| 55 |
iface.launch()
|