PRSHNTKUMR commited on
Commit
667399c
·
verified ·
1 Parent(s): 0ecef97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -37
app.py CHANGED
@@ -1,39 +1,44 @@
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import gradio as gr
3
- import google.generativeai as genai
4
-
5
- # Replace with your actual API key
6
- genai.configure(api_key='GOOGLE_API_KEY')
7
-
8
- chat_history = [] # Initialize chat history
9
-
10
- def generate_text(prompt):
11
- """Generates text using Gemini Pro and updates chat history."""
12
-
13
- global chat_history
14
-
15
- chat_history.append({"role": "user", "content": prompt}) # Add user message
16
-
17
- response = genai.text.generate_text(
18
- model='models/gemini-pro',
19
- prompt="\n".join([f"{m['role']}: {m['content']}" for m in chat_history]),
20
- temperature=0.7,
21
- max_output_tokens=1024,
22
- )
23
-
24
- chat_history.append({"role": "assistant", "content": response.text}) # Add assistant message
25
-
26
- # Format chat history for display
27
- chat_display = "\n".join([f"{m['role']}: {m['content']}" for m in chat_history])
28
-
29
- return chat_display
30
-
31
- iface = gr.Interface(
32
- fn=generate_text,
33
- inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
34
- outputs="text",
35
- title="Gemini Pro Chatbot with History",
36
- description="Ask me anything!",
37
- )
38
-
39
- iface.launch()
 
1
 
2
+ import openai
3
+ import os
4
+ from dotenv import load_dotenv, find_dotenv
5
+
6
+ _ = load_dotenv(find_dotenv()) # read local .env file
7
+
8
+ openai.api_key = os.getenv('OPENAI_API_KEY')
9
+
10
+
11
+ def get_completion_from_messages(messages,
12
+ model="gpt-3.5-turbo",
13
+ temperature=0,
14
+ max_tokens=500):
15
+ response = openai.chat.completions.create( # Use openai.chat.completions.create instead of openai.ChatCompletion.create
16
+ model=model,
17
+ messages=messages,
18
+ temperature=temperature,
19
+ max_tokens=max_tokens,
20
+ )
21
+ return response.choices[0].message.content
22
  import gradio as gr
23
+ def generate(input, slider):
24
+ messages = [
25
+ {'role':'user',
26
+ 'content': f"{input}"},
27
+ ]
28
+
29
+ output = get_completion_from_messages(messages)
30
+ return output
31
+
32
+ demo = gr.ChatInterface(
33
+ fn=generate,
34
+ chatbot=gr.Chatbot(height=300),
35
+ textbox=gr.Textbox(placeholder="Ask me a yes or no question", container=False, scale=7),
36
+ title="Your Helper CHAT Bot",
37
+ description="Ask 'Yes Man' any question",
38
+ theme="soft",
39
+ examples=["Hello", "Am I cool?", "Are tomatoes vegetables?"],
40
+ cache_examples=True,
41
+ #retry_btn=None,
42
+ #undo_btn="Delete Previous",
43
+ #clear_btn="Clear",
44
+ ).launch()