antfraia commited on
Commit
02dd212
·
1 Parent(s): a20d2af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -40
app.py CHANGED
@@ -1,12 +1,9 @@
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
@@ -21,10 +18,8 @@ def get_weather(city_name, api_key):
21
  main_data = data.get("main", {})
22
  weather_data = data.get("weather", [{}])[0]
23
  temperature = kelvin_to_celsius(main_data.get("temp", 0))
24
- pressure = main_data.get("pressure", "N/A")
25
- humidity = main_data.get("humidity", "N/A")
26
  weather_description = weather_data.get("description", "N/A")
27
- return temperature, pressure, humidity, weather_description
28
  else:
29
  return None
30
 
@@ -42,54 +37,35 @@ def openai_create(prompt):
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"""
57
- {city1}:
58
- Temperature: {weather1[0]:.2f}°C
59
- Pressure: {weather1[1]} hPa
60
- Humidity: {weather1[2]}%
61
- Condition: {weather1[3]}
62
 
63
- {city2}:
64
- Temperature: {weather2[0]:.2f}°C
65
- Pressure: {weather2[1]} hPa
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)
 
1
  import gradio as gr
2
  import requests
3
 
4
+ # Constants
5
  API_KEY = '91b23cab82ee530b2052c8757e343b0d'
6
  OPENAI_API_KEY = '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'
 
 
 
7
 
8
  def kelvin_to_celsius(temp_kelvin):
9
  return temp_kelvin - 273.15
 
18
  main_data = data.get("main", {})
19
  weather_data = data.get("weather", [{}])[0]
20
  temperature = kelvin_to_celsius(main_data.get("temp", 0))
 
 
21
  weather_description = weather_data.get("description", "N/A")
22
+ return temperature, weather_description
23
  else:
24
  return None
25
 
 
37
  "top_p": 1,
38
  "frequency_penalty": 0,
39
  "presence_penalty": 0.6,
40
+ "stop": ["\n"]
41
  }
42
  response = requests.post(openai_url, headers=headers, json=data)
43
  response_data = response.json()
44
  return response_data['choices'][0]['text'].strip()
45
 
46
+ def summarize_weather(city1, city2):
47
  weather1 = get_weather(city1, API_KEY)
48
  weather2 = get_weather(city2, API_KEY)
49
 
50
  if weather1 and weather2:
51
  comparison = f"""
52
+ In {city1}, the current weather is {weather1[1]} with a temperature of {weather1[0]:.2f}°C.
 
 
 
 
53
 
54
+ Meanwhile, in {city2}, it's {weather2[1]} and the temperature is {weather2[0]:.2f}°C.
 
 
 
 
55
  """
56
  summary = openai_create(comparison)
57
  return summary
58
  else:
59
  return "Error fetching weather data for one or both cities."
60
 
61
+ # Gradio interface
62
+ interface = gr.Interface(
63
+ fn=summarize_weather,
64
+ inputs=[
65
+ gr.Textbox(default="New York", placeholder="Enter City 1"),
66
+ gr.Textbox(default="Los Angeles", placeholder="Enter City 2")
67
+ ],
68
+ outputs="text"
69
+ )
70
 
71
+ interface.launch(debug=True)