Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import openai
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def kelvin_to_celsius(temp_kelvin):
|
| 8 |
return temp_kelvin - 273.15
|
|
@@ -24,15 +28,29 @@ def get_weather(city_name, api_key):
|
|
| 24 |
else:
|
| 25 |
return None
|
| 26 |
|
| 27 |
-
def
|
| 28 |
-
"""
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def compare_weather(city1, city2):
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
weather2 = get_weather(city2, api_key)
|
| 36 |
|
| 37 |
if weather1 and weather2:
|
| 38 |
comparison = f"""
|
|
@@ -48,22 +66,30 @@ def compare_weather(city1, city2):
|
|
| 48 |
Humidity: {weather2[2]}%
|
| 49 |
Condition: {weather2[3]}
|
| 50 |
"""
|
| 51 |
-
summary =
|
| 52 |
return summary
|
| 53 |
else:
|
| 54 |
return "Error fetching weather data for one or both cities."
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
inputs=[
|
| 62 |
-
gr.components.Dropdown(choices=cities, label="City 1"),
|
| 63 |
-
gr.components.Dropdown(choices=cities, label="City 2")
|
| 64 |
-
],
|
| 65 |
-
outputs="text"
|
| 66 |
-
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
+
# Set constants
|
| 5 |
+
API_KEY = '91b23cab82ee530b2052c8757e343b0d'
|
| 6 |
+
OPENAI_API_KEY = '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'
|
| 7 |
+
START_SEQUENCE = "\nAI:"
|
| 8 |
+
RESTART_SEQUENCE= "\nHuman:"
|
| 9 |
+
PROMPT = "The following is a conversation with AI assistant. The assistant is helpful, kind, witty, and clever."
|
| 10 |
|
| 11 |
def kelvin_to_celsius(temp_kelvin):
|
| 12 |
return temp_kelvin - 273.15
|
|
|
|
| 28 |
else:
|
| 29 |
return None
|
| 30 |
|
| 31 |
+
def openai_create(prompt):
|
| 32 |
+
"""Use OpenAI API directly to get response."""
|
| 33 |
+
openai_url = "https://api.openai.com/v1/engines/text-davinci-003/completions"
|
| 34 |
+
headers = {
|
| 35 |
+
"Authorization": f"Bearer {OPENAI_API_KEY}",
|
| 36 |
+
"Content-Type": "application/json"
|
| 37 |
+
}
|
| 38 |
+
data = {
|
| 39 |
+
"prompt": prompt,
|
| 40 |
+
"temperature": 0.7,
|
| 41 |
+
"max_tokens": 256,
|
| 42 |
+
"top_p": 1,
|
| 43 |
+
"frequency_penalty": 0,
|
| 44 |
+
"presence_penalty": 0.6,
|
| 45 |
+
"stop": ["Human:", "AI:"]
|
| 46 |
+
}
|
| 47 |
+
response = requests.post(openai_url, headers=headers, json=data)
|
| 48 |
+
response_data = response.json()
|
| 49 |
+
return response_data['choices'][0]['text'].strip()
|
| 50 |
|
| 51 |
def compare_weather(city1, city2):
|
| 52 |
+
weather1 = get_weather(city1, API_KEY)
|
| 53 |
+
weather2 = get_weather(city2, API_KEY)
|
|
|
|
| 54 |
|
| 55 |
if weather1 and weather2:
|
| 56 |
comparison = f"""
|
|
|
|
| 66 |
Humidity: {weather2[2]}%
|
| 67 |
Condition: {weather2[3]}
|
| 68 |
"""
|
| 69 |
+
summary = openai_create(comparison)
|
| 70 |
return summary
|
| 71 |
else:
|
| 72 |
return "Error fetching weather data for one or both cities."
|
| 73 |
|
| 74 |
+
def chatgpt_clone(input, history):
|
| 75 |
+
"""Function to simulate chat with GPT-like behavior."""
|
| 76 |
+
history = history or []
|
| 77 |
+
s = list(sum(history, ()))
|
| 78 |
+
s.append(input)
|
| 79 |
+
inp = ' '.join(s)
|
| 80 |
+
output = openai_create(inp)
|
| 81 |
+
history.append((input, output))
|
| 82 |
+
return history, history
|
| 83 |
|
| 84 |
+
# Gradio Interface
|
| 85 |
+
block = gr.Blocks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
with block:
|
| 88 |
+
gr.Markdown("""<h1><center> CHATGPT PRIVATE ASSISTANT WITH OPENAI AND GRADIO</center></h1>""")
|
| 89 |
+
chatbot = gr.Chatbot()
|
| 90 |
+
message = gr.Textbox(placeholder=PROMPT)
|
| 91 |
+
state = gr.State()
|
| 92 |
+
submit = gr.Button("SEND")
|
| 93 |
+
submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot, state])
|
| 94 |
+
|
| 95 |
+
block.launch(debug=True)
|