duduvicky commited on
Commit
693dc1a
·
verified ·
1 Parent(s): f7b2e6e

update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -33
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 a smaller, faster model (TinyLlama-1.1B-Chat, optimized for CPU)
9
  llm = HuggingFacePipeline.from_model_id(
10
- model_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
11
  task="text-generation",
12
  pipeline_kwargs={
13
- "max_new_tokens": 150, # Shorter responses for speed
14
  "do_sample": True,
15
- "temperature": 0.6, # Slightly less creative for consistency
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
- # Optional: Web search tool (comment out if you don’t need it for faster responses)
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 # Disable logging for speed
40
  )
41
 
42
- # Function to handle chat and optional tool use
43
  def chat_with_agent(message, history):
44
  try:
45
- # Check if the query likely needs a web search (e.g., current events)
46
- if any(keyword in message.lower() for keyword in ["latest", "news", "current", "today", "weather"]):
47
- search_result = search.run(message)
48
- prompt = f"User asked: {message}\nWeb search result: {search_result}\nAnswer based on this info."
49
- response = conversation.predict(input=prompt)
50
- else:
51
- response = conversation.predict(input=message)
52
  except Exception as e:
53
- response = f"Error: {str(e)}. Try rephrasing your question."
54
- return response
55
 
56
  # Gradio chat interface
57
  iface = gr.ChatInterface(
58
  fn=chat_with_agent,
59
- title="Fast Free AI Agent",
60
- description="A lightweight conversational AI that remembers our talks and can search the web. Hosted free on Hugging Face Spaces. Responses should be faster (5-15 seconds).",
61
- examples=["What's my name if I told you it's Alex?", "Tell me a quick joke.", "What's the latest AI news?"]
 
 
 
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()