Bijay13 commited on
Commit
77da7bd
·
1 Parent(s): 7ae1f42

removed redundant code

Browse files
Files changed (1) hide show
  1. app.py +10 -27
app.py CHANGED
@@ -5,7 +5,6 @@ from langchain.memory import ConversationBufferMemory
5
  from langchain.chains import ConversationChain
6
  from langchain.prompts import PromptTemplate
7
 
8
- # Constants
9
  MODEL_NAME = "llama-3.3-70b-versatile"
10
  DEFAULT_API_KEY = os.getenv("GROQ_API_KEY", "")
11
 
@@ -34,18 +33,16 @@ AI Assistant:"""
34
  template=template
35
  )
36
 
37
- conversation = ConversationChain(
38
  llm=llm,
39
  memory=memory,
40
  prompt=prompt,
41
  verbose=False
42
  )
43
-
44
- return conversation
45
 
46
  conversation_chain = None
47
 
48
- def chat_function(message, history, api_key):
49
  global conversation_chain
50
 
51
  if not api_key:
@@ -58,37 +55,29 @@ def chat_function(message, history, api_key):
58
  return f"Error initializing chatbot: {str(e)}"
59
 
60
  try:
61
- response = conversation_chain.predict(input=message)
62
- return response
63
  except Exception as e:
64
  return f"Error: {str(e)}"
65
 
66
  def reset_conversation():
67
  global conversation_chain
68
  conversation_chain = None
69
- return None
70
 
71
  with gr.Blocks(title="LLM based Chatbot") as demo:
72
  gr.Markdown("# 🤖 LLM based Chatbot")
73
  gr.Markdown("Chat with an AI assistant powered by LangChain and Groq")
74
  gr.Markdown(f"**Model:** `{MODEL_NAME}`")
75
 
76
- # Only show API key input if not set in environment
77
  if not DEFAULT_API_KEY:
78
- with gr.Row():
79
- api_key_input = gr.Textbox(
80
- label="Groq API Key",
81
- placeholder="Enter your Groq API key here...",
82
- type="password",
83
- value=""
84
- )
85
  else:
86
- # Use hidden component with default key from environment
87
  api_key_input = gr.Textbox(
88
- label="Groq API Key (Loaded from Environment)",
89
  type="password",
90
  value=DEFAULT_API_KEY,
91
- interactive=False,
92
  visible=False
93
  )
94
 
@@ -102,20 +91,14 @@ with gr.Blocks(title="LLM based Chatbot") as demo:
102
  )
103
  submit_btn = gr.Button("Send", scale=1)
104
 
105
- with gr.Row():
106
- clear_btn = gr.Button("Clear Conversation")
107
 
108
  def respond(message, chat_history, api_key):
109
  if not message.strip():
110
  return chat_history, ""
111
 
112
- # Add user message
113
  chat_history.append({"role": "user", "content": message})
114
-
115
- # Get bot response
116
- bot_message = chat_function(message, chat_history, api_key)
117
-
118
- # Add assistant message
119
  chat_history.append({"role": "assistant", "content": bot_message})
120
 
121
  return chat_history, ""
 
5
  from langchain.chains import ConversationChain
6
  from langchain.prompts import PromptTemplate
7
 
 
8
  MODEL_NAME = "llama-3.3-70b-versatile"
9
  DEFAULT_API_KEY = os.getenv("GROQ_API_KEY", "")
10
 
 
33
  template=template
34
  )
35
 
36
+ return ConversationChain(
37
  llm=llm,
38
  memory=memory,
39
  prompt=prompt,
40
  verbose=False
41
  )
 
 
42
 
43
  conversation_chain = None
44
 
45
+ def chat_function(message, api_key):
46
  global conversation_chain
47
 
48
  if not api_key:
 
55
  return f"Error initializing chatbot: {str(e)}"
56
 
57
  try:
58
+ return conversation_chain.predict(input=message)
 
59
  except Exception as e:
60
  return f"Error: {str(e)}"
61
 
62
  def reset_conversation():
63
  global conversation_chain
64
  conversation_chain = None
 
65
 
66
  with gr.Blocks(title="LLM based Chatbot") as demo:
67
  gr.Markdown("# 🤖 LLM based Chatbot")
68
  gr.Markdown("Chat with an AI assistant powered by LangChain and Groq")
69
  gr.Markdown(f"**Model:** `{MODEL_NAME}`")
70
 
 
71
  if not DEFAULT_API_KEY:
72
+ api_key_input = gr.Textbox(
73
+ label="Groq API Key",
74
+ placeholder="Enter your Groq API key here...",
75
+ type="password"
76
+ )
 
 
77
  else:
 
78
  api_key_input = gr.Textbox(
 
79
  type="password",
80
  value=DEFAULT_API_KEY,
 
81
  visible=False
82
  )
83
 
 
91
  )
92
  submit_btn = gr.Button("Send", scale=1)
93
 
94
+ clear_btn = gr.Button("Clear Conversation")
 
95
 
96
  def respond(message, chat_history, api_key):
97
  if not message.strip():
98
  return chat_history, ""
99
 
 
100
  chat_history.append({"role": "user", "content": message})
101
+ bot_message = chat_function(message, api_key)
 
 
 
 
102
  chat_history.append({"role": "assistant", "content": bot_message})
103
 
104
  return chat_history, ""