update app.py
Browse files
app.py
CHANGED
|
@@ -2,64 +2,51 @@ import gradio as gr
|
|
| 2 |
from langchain_huggingface import HuggingFacePipeline
|
| 3 |
from langchain.chains import ConversationChain
|
| 4 |
from langchain.memory import ConversationBufferMemory
|
| 5 |
-
from langchain_community.tools import DuckDuckGoSearchRun
|
| 6 |
-
from langchain_core.tools import Tool
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
llm = HuggingFacePipeline.from_model_id(
|
| 10 |
-
model_id="
|
| 11 |
task="text-generation",
|
| 12 |
pipeline_kwargs={
|
| 13 |
-
"max_new_tokens":
|
| 14 |
"do_sample": True,
|
| 15 |
-
"temperature": 0.
|
| 16 |
"top_k": 40,
|
| 17 |
"top_p": 0.9
|
| 18 |
-
}
|
| 19 |
-
model_kwargs={"trust_remote_code": True}
|
| 20 |
)
|
| 21 |
|
| 22 |
# Set up conversation memory
|
| 23 |
memory = ConversationBufferMemory()
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
search = DuckDuckGoSearchRun()
|
| 27 |
-
tools = [
|
| 28 |
-
Tool(
|
| 29 |
-
name="Web Search",
|
| 30 |
-
func=search.run,
|
| 31 |
-
description="Use for current events or facts. Input a search query."
|
| 32 |
-
)
|
| 33 |
-
]
|
| 34 |
-
|
| 35 |
-
# Create a simple conversation chain (faster than full agent)
|
| 36 |
conversation = ConversationChain(
|
| 37 |
llm=llm,
|
| 38 |
memory=memory,
|
| 39 |
-
verbose=False #
|
| 40 |
)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
def chat_with_agent(message, history):
|
| 44 |
try:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
response = conversation.predict(input=prompt)
|
| 50 |
-
else:
|
| 51 |
-
response = conversation.predict(input=message)
|
| 52 |
except Exception as e:
|
| 53 |
-
|
| 54 |
-
return response
|
| 55 |
|
| 56 |
# Gradio chat interface
|
| 57 |
iface = gr.ChatInterface(
|
| 58 |
fn=chat_with_agent,
|
| 59 |
-
title="
|
| 60 |
-
description="
|
| 61 |
-
examples=[
|
|
|
|
|
|
|
|
|
|
| 62 |
)
|
| 63 |
|
|
|
|
| 64 |
if __name__ == "__main__":
|
| 65 |
iface.launch()
|
|
|
|
| 2 |
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": 80, # Very short responses for speed
|
| 12 |
"do_sample": True,
|
| 13 |
+
"temperature": 0.7, # Balanced creativity
|
| 14 |
"top_k": 40,
|
| 15 |
"top_p": 0.9
|
| 16 |
+
}
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
# Set up conversation memory
|
| 20 |
memory = ConversationBufferMemory()
|
| 21 |
|
| 22 |
+
# Create a simple conversation chain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
conversation = ConversationChain(
|
| 24 |
llm=llm,
|
| 25 |
memory=memory,
|
| 26 |
+
verbose=False # No logging for speed
|
| 27 |
)
|
| 28 |
|
| 29 |
+
# Chat function
|
| 30 |
def chat_with_agent(message, history):
|
| 31 |
try:
|
| 32 |
+
response = conversation.predict(input=message)
|
| 33 |
+
# Clean up response to avoid verbosity
|
| 34 |
+
response = response.strip().split("\n")[0][:200] # Limit to 200 chars
|
| 35 |
+
return response
|
|
|
|
|
|
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
+
return f"Oops, something went wrong: {str(e)}. Try again!"
|
|
|
|
| 38 |
|
| 39 |
# Gradio chat interface
|
| 40 |
iface = gr.ChatInterface(
|
| 41 |
fn=chat_with_agent,
|
| 42 |
+
title="Your AI Chat Buddy",
|
| 43 |
+
description="I'm here to talk with you and remember our conversation! Ask me anything, and I'll respond in a few seconds. Hosted free on Hugging Face Spaces.",
|
| 44 |
+
examples=[
|
| 45 |
+
"My name is Alex. What's my name?",
|
| 46 |
+
"Tell me a quick joke."
|
| 47 |
+
]
|
| 48 |
)
|
| 49 |
|
| 50 |
+
# Launch the app
|
| 51 |
if __name__ == "__main__":
|
| 52 |
iface.launch()
|