kathirog commited on
Commit
3a0ec3f
·
verified ·
1 Parent(s): c77361a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -85
app.py CHANGED
@@ -1,63 +1,68 @@
1
  import gradio as gr
2
- import torch
3
- from transformers import AutoTokenizer, AutoModelForCausalLM
4
- import pyttsx3
5
  import speech_recognition as sr
6
- from huggingface_hub import InferenceClient
7
 
8
- # API Key for HuggingFace InferenceClient
9
- API_KEY = "AIzaSyBWBxsPBykuJ6z_kMYlAq9k9u3YU2Uy8Oc"
10
 
11
- # Initialize the InferenceClient (replace with your model name if necessary)
12
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=API_KEY)
13
 
14
- # Hardcoded system message
15
- system_message = "You are a friendly and helpful chatbot."
16
 
17
- # Load model with quantization and auto-device setup for faster loading
18
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
19
- model = AutoModelForCausalLM.from_pretrained(
20
- "eurecom-ds/Phi-3-mini-4k-socratic", # Replace with your model
21
- torch_dtype=torch.bfloat16,
22
- load_in_4bit=True, # Enable 4-bit quantization for faster inference
23
- device_map="auto", # Automatically use GPU if available
24
- )
25
 
26
- # Tokenizer for the model
27
- tokenizer = AutoTokenizer.from_pretrained("eurecom-ds/Phi-3-mini-4k-socratic")
 
 
 
28
 
29
- # Function to handle text responses
30
- def respond(message, history: list, audio_input=None):
 
 
 
 
 
 
 
 
 
 
31
  if audio_input:
32
  message = voice_to_text(audio_input)
33
 
34
- messages = [{"role": "system", "content": system_message}]
 
35
 
 
36
  for val in history:
37
- if val[0]:
38
  messages.append({"role": "user", "content": val[0]})
39
- if val[1]:
40
  messages.append({"role": "assistant", "content": val[1]})
41
-
 
42
  messages.append({"role": "user", "content": message})
43
 
44
- response = "" # Initialize response
45
-
46
- try:
47
- for message_response in client.chat_completion(messages, max_tokens=150, stream=True): # Reduce max tokens for faster response
48
- if 'choices' in message_response and len(message_response['choices']) > 0:
49
- delta_content = message_response['choices'][0].get('delta', {}).get('content', '')
50
- if delta_content:
51
- response += delta_content
52
- else:
53
- print("Error: No valid content in response")
54
- break
55
- except Exception as e:
56
- print(f"Error during API request: {e}")
57
 
58
  return response
59
 
60
-
61
  # Convert voice input (audio) to text
62
  def voice_to_text(audio):
63
  recognizer = sr.Recognizer()
@@ -78,50 +83,18 @@ def text_to_voice(text):
78
  engine.runAndWait()
79
  return 'response.mp3'
80
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
- # Gradio Interface
83
- def create_interface():
84
- with gr.Blocks() as demo:
85
- chatbot = gr.Chatbot()
86
- msg = gr.Textbox(label="Enter your message")
87
- clear = gr.Button("Clear")
88
-
89
- # Inputs and Outputs for Text and Audio
90
- with gr.Row():
91
- text_input = gr.Textbox(label="Text Input", placeholder="Enter your message...")
92
- audio_input = gr.Audio(type="filepath", label="Audio Input (Optional)")
93
-
94
- # Outputs for Text and Audio Response
95
- with gr.Row():
96
- text_output = gr.Textbox(label="Text Output")
97
- audio_output = gr.Audio(label="Voice Output")
98
-
99
- # Interaction logic
100
- def user(user_message, history):
101
- return "", history + [[user_message, ""]]
102
-
103
- def bot(history):
104
- user_query = "".join(f"Student: {s}\nTeacher: {t}\n" for s, t in history[:-1])
105
- last_query = history[-1][0]
106
- user_query += f"Student: {last_query}"
107
- response = respond(user_query, history)
108
- history[-1][1] = response
109
- return history, response # Return updated history and response
110
-
111
- # Submit text input
112
- msg.submit(user, [msg, chatbot], [msg, chatbot]).then(bot, [chatbot], [chatbot, text_output])
113
-
114
- # Submit audio input
115
- audio_input.change(user, [audio_input, chatbot], [audio_input, chatbot]).then(bot, [chatbot], [chatbot, text_output])
116
-
117
- # Clear button
118
- clear.click(lambda: None, None, chatbot, queue=False)
119
-
120
- return demo
121
-
122
-
123
- # Launch Gradio app
124
  if __name__ == "__main__":
125
- demo = create_interface()
126
- demo.queue()
127
- demo.launch(server_name="0.0.0.0", server_port=2121) # You can change port as needed
 
1
  import gradio as gr
2
+ import requests
3
+ import os
4
+ from dotenv import load_dotenv
5
  import speech_recognition as sr
6
+ import pyttsx3
7
 
8
+ # Load environment variables (assuming Gemini API key is stored in a .env file)
9
+ load_dotenv()
10
 
11
+ # Replace this with the actual API URL for Gemini
12
+ GEMINI_API_URL = "https://api.gemini.com/v1/query" # You need to replace this URL
13
 
14
+ # Retrieve your API key securely from environment variables
15
+ API_KEY = os.getenv("GEMINI_API_KEY")
16
 
17
+ # Function to send messages to Gemini API
18
+ def call_gemini_api(messages):
19
+ headers = {
20
+ "Authorization": f"Bearer {API_KEY}",
21
+ "Content-Type": "application/json",
22
+ }
 
 
23
 
24
+ # Create the request body based on your messages (adjust format as per Gemini's API)
25
+ data = {
26
+ "messages": messages,
27
+ "max_tokens": 512, # Adjust token length as needed
28
+ }
29
 
30
+ try:
31
+ # Making the POST request to the Gemini API URL
32
+ response = requests.post(GEMINI_API_URL, headers=headers, json=data)
33
+ response.raise_for_status()
34
+ return response.json() # Assuming Gemini returns JSON response
35
+ except requests.exceptions.RequestException as e:
36
+ print(f"Error calling Gemini API: {e}")
37
+ return {"error": str(e)}
38
+
39
+ # Define the respond function
40
+ def respond(message, history: list[tuple[str, str]], audio_input=None):
41
+ # If audio input is provided, convert it to text
42
  if audio_input:
43
  message = voice_to_text(audio_input)
44
 
45
+ # Prepare the messages for the Gemini API
46
+ messages = [{"role": "system", "content": "You are a friendly and helpful chatbot."}]
47
 
48
+ # Add history to the messages list
49
  for val in history:
50
+ if val[0]: # Check if user message exists
51
  messages.append({"role": "user", "content": val[0]})
52
+ if val[1]: # Check if assistant message exists
53
  messages.append({"role": "assistant", "content": val[1]})
54
+
55
+ # Append the current user message
56
  messages.append({"role": "user", "content": message})
57
 
58
+ # Get the Gemini API response
59
+ response_data = call_gemini_api(messages)
60
+
61
+ # Assuming Gemini API returns a 'content' key in the response
62
+ response = response_data.get("content", "Sorry, I could not process your request.")
 
 
 
 
 
 
 
 
63
 
64
  return response
65
 
 
66
  # Convert voice input (audio) to text
67
  def voice_to_text(audio):
68
  recognizer = sr.Recognizer()
 
83
  engine.runAndWait()
84
  return 'response.mp3'
85
 
86
+ # Create the Gradio interface
87
+ demo = gr.Interface(
88
+ fn=respond,
89
+ inputs=[
90
+ gr.Textbox(label="Text Input (or leave blank to use audio input)", placeholder="Enter your message here..."), # Text input
91
+ gr.Audio(type="filepath", label="Audio Input (or leave blank to use text input)"), # Audio input
92
+ ],
93
+ outputs=[
94
+ gr.Textbox(label="Text Output"), # Text output
95
+ gr.Audio(label="Voice Output") # Voice output
96
+ ]
97
+ )
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  if __name__ == "__main__":
100
+ demo.launch()