RootDeveloperDS commited on
Commit
7b8c245
Β·
verified Β·
1 Parent(s): 63fb958

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -8
app.py CHANGED
@@ -1,34 +1,53 @@
 
 
1
  import os
2
  import gradio as gr
3
  from langchain.chat_models import ChatOpenAI
4
  from langchain import LLMChain, PromptTemplate
5
  from langchain.memory import ConversationBufferMemory
6
 
7
- OPENAI_API_KEY=os.getenv('OPENAI_API_KEY')
 
 
 
 
 
8
 
9
- template = """You are a helpful assistant to answer user queries and coding questions,act like a best programmer and explain evry thing like explaining it to a 5 year child.
 
 
10
  {chat_history}
11
  User: {user_message}
12
  Chatbot:"""
13
 
 
14
  prompt = PromptTemplate(
15
  input_variables=["chat_history", "user_message"], template=template
16
  )
17
 
 
18
  memory = ConversationBufferMemory(memory_key="chat_history")
19
 
 
20
  llm_chain = LLMChain(
21
- llm=ChatOpenAI(temperature='0.5', model_name="gpt-3.5-turbo"),
22
  prompt=prompt,
23
  verbose=True,
24
  memory=memory,
25
  )
26
 
27
- def get_text_response(user_message,history):
28
- response = llm_chain.predict(user_message = user_message)
 
 
29
  return response
30
 
31
- demo = gr.ChatInterface(get_text_response)
 
 
 
 
 
 
32
 
33
- if __name__ == "__main__":
34
- demo.launch(share=True) #To create a public link, set `share=True` in `launch()`. To enable errors and logs, set `debug=True` in `launch()`.
 
1
+
2
+
3
  import os
4
  import gradio as gr
5
  from langchain.chat_models import ChatOpenAI
6
  from langchain import LLMChain, PromptTemplate
7
  from langchain.memory import ConversationBufferMemory
8
 
9
+ # βœ… Correct way to fetch the API key
10
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
11
+
12
+ # βœ… This MUST be passed explicitly if running outside OpenAI-hosted envs
13
+ if not OPENAI_API_KEY:
14
+ raise ValueError("OPENAI_API_KEY is not set in the environment variables.")
15
 
16
+ # βœ… Template
17
+ template = """You are a helpful assistant to answer user queries and coding questions.
18
+ Act like the best programmer and explain everything like explaining it to a 5-year-old.
19
  {chat_history}
20
  User: {user_message}
21
  Chatbot:"""
22
 
23
+ # βœ… Prompt
24
  prompt = PromptTemplate(
25
  input_variables=["chat_history", "user_message"], template=template
26
  )
27
 
28
+ # βœ… Memory
29
  memory = ConversationBufferMemory(memory_key="chat_history")
30
 
31
+ # ❌ FIXED: temperature must be a float, not a string
32
  llm_chain = LLMChain(
33
+ llm=ChatOpenAI(temperature=0.5, model_name="gpt-3.5-turbo", openai_api_key=OPENAI_API_KEY),
34
  prompt=prompt,
35
  verbose=True,
36
  memory=memory,
37
  )
38
 
39
+ # βœ… Function needs to return a response AND history (for gr.ChatInterface)
40
+ def get_text_response(user_message, history):
41
+ response = llm_chain.predict(user_message=user_message)
42
+ # History must be returned for the Gradio chat interface to maintain context
43
  return response
44
 
45
+ # βœ… Create interface
46
+ demo = gr.ChatInterface(fn=get_text_response)
47
+
48
+ # βœ… Required for Hugging Face Spaces (they call this file)
49
+ demo.launch()
50
+
51
+
52
 
53
+