resolverkatla commited on
Commit
40212a8
·
1 Parent(s): a81180f
Files changed (2) hide show
  1. app.py +18 -23
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,34 +1,29 @@
1
  import gradio as gr
2
- import openai
3
  import os
4
 
5
- # Set your OpenAI API key (or use environment variables)
6
- openai.api_key = "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"
 
7
 
8
- def chatbot(prompt, history=[]):
9
- """Generates a chatbot response using OpenAI's API."""
10
- messages = [{"role": "system", "content": "You are a helpful assistant."}]
11
- for user_msg, bot_msg in history:
12
- messages.append({"role": "user", "content": user_msg})
13
- messages.append({"role": "assistant", "content": bot_msg})
14
-
15
- messages.append({"role": "user", "content": prompt})
16
-
17
- response = openai.ChatCompletion.create(
18
- model="gpt-4",
19
- messages=messages
20
- )
21
 
22
- reply = response["choices"][0]["message"]["content"]
23
- return reply
 
 
 
 
 
24
 
25
- # Create a Gradio interface
26
  chat_interface = gr.ChatInterface(
27
  chatbot,
28
- title="Simple OpenAI Chatbot",
29
- description="A chatbot powered by OpenAI's GPT API. Start a conversation!",
30
  )
31
 
32
- # Launch on Hugging Face Spaces
33
  if __name__ == "__main__":
34
- chat_interface.launch()
 
1
  import gradio as gr
2
+ import google.generativeai as genai
3
  import os
4
 
5
+ # Set up Gemini API key (use environment variable on Hugging Face)
6
+ API_KEY = os.getenv("GEMINI_API_KEY") # Set this in Hugging Face secrets
7
+ genai.configure(api_key=API_KEY)
8
 
9
+ # Use Gemini 1.5 Flash (free-tier accessible)
10
+ model = genai.GenerativeModel("gemini-1.5-flash")
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ def chatbot(prompt, history=[]):
13
+ """Generates a chatbot response using the free-tier Gemini API."""
14
+ try:
15
+ response = model.generate_content(prompt)
16
+ return response.text
17
+ except Exception as e:
18
+ return f"Error: {e}"
19
 
20
+ # Create Gradio chatbot interface
21
  chat_interface = gr.ChatInterface(
22
  chatbot,
23
+ title="Free Gemini API Chatbot",
24
+ description="A chatbot powered by the free-tier Google Gemini API.",
25
  )
26
 
27
+ # Run the chatbot
28
  if __name__ == "__main__":
29
+ chat_interface.launch()
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
- openai
2
  gradio
 
1
+ google-generativeai
2
  gradio