aheedsajid commited on
Commit
79008a0
1 Parent(s): 77f773d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -25
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
  import gradio as gr
3
- import requests
4
  from dotenv import load_dotenv
5
  from gradio_client import Client, file # Import Gradio client
6
 
@@ -8,45 +8,52 @@ from gradio_client import Client, file # Import Gradio client
8
  load_dotenv()
9
 
10
  # Retrieve API key from environment variable
11
- API_KEY = os.getenv("API_KEY")
12
 
13
  # Retrieve system content from environment variable
14
  SYSTEM_CONTENT = os.getenv("SYSTEM_CONTENT")
15
 
16
- API_URL = "https://api.openai.com/v1/chat/completions"
17
-
18
  # Initialize conversation history
19
  conversation_history = []
20
 
21
  # Initialize Gradio client for new TTS API
22
  tts_client = Client("LeeSangHoon/HierSpeech_TTS")
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def generate_response(user_input):
25
  global conversation_history
26
 
27
  # Add user message to conversation history
28
  conversation_history.append({"role": "user", "content": user_input})
29
 
30
- # Send last 5 messages in conversation history to API
31
- messages = conversation_history[-5:]
32
 
33
- # Add system message to messages
34
- messages.insert(0, {"role": "system", "content": SYSTEM_CONTENT})
35
-
36
- headers = {
37
- "Authorization": f"Bearer {API_KEY}",
38
- "Content-Type": "application/json"
39
- }
40
-
41
- data = {
42
- "model": "gpt-3.5-turbo",
43
- "messages": messages
44
- }
45
-
46
- response = requests.post(API_URL, headers=headers, json=data)
47
- response.raise_for_status() # Raise an error for bad status codes
48
 
49
- response_text = response.json()['choices'][0]['message']['content']
 
 
50
 
51
  # Add assistant response to conversation history
52
  conversation_history.append({"role": "assistant", "content": response_text})
@@ -57,8 +64,8 @@ def generate_response(user_input):
57
  file("audio.mp3"), # Path to the local audio file
58
  0.333, # ttv_temperature
59
  0.333, # vc_temperature
60
- 1, # duratuion_temperature
61
- 1, # duratuion_length
62
  0, # denoise_ratio
63
  0, # random_seed
64
  api_name="/predict"
@@ -76,4 +83,4 @@ iface = gr.Interface(
76
  description="Contact me if you want another character/voice<br>WhatsApp me: +92-332-4399819<br> Email me: aheedsajid@gmail.com<br><b>Donate something to increase GPU power</b><br>[Click here to Donate](https://nowpayments.io/donation/aheed)"
77
  )
78
 
79
- iface.launch()
 
1
  import os
2
  import gradio as gr
3
+ import google.generativeai as genai
4
  from dotenv import load_dotenv
5
  from gradio_client import Client, file # Import Gradio client
6
 
 
8
  load_dotenv()
9
 
10
  # Retrieve API key from environment variable
11
+ API_KEY = os.getenv("GEMINI_API_KEY")
12
 
13
  # Retrieve system content from environment variable
14
  SYSTEM_CONTENT = os.getenv("SYSTEM_CONTENT")
15
 
 
 
16
  # Initialize conversation history
17
  conversation_history = []
18
 
19
  # Initialize Gradio client for new TTS API
20
  tts_client = Client("LeeSangHoon/HierSpeech_TTS")
21
 
22
+ # Configure the Google Generative AI model
23
+ genai.configure(api_key=API_KEY)
24
+
25
+ generation_config = {
26
+ "temperature": 1,
27
+ "top_p": 0.95,
28
+ "top_k": 64,
29
+ "max_output_tokens": 8192,
30
+ "response_mime_type": "text/plain",
31
+ }
32
+
33
+ model = genai.GenerativeModel(
34
+ model_name="gemini-1.5-pro",
35
+ generation_config=generation_config,
36
+ )
37
+
38
  def generate_response(user_input):
39
  global conversation_history
40
 
41
  # Add user message to conversation history
42
  conversation_history.append({"role": "user", "content": user_input})
43
 
44
+ # Add system message to the beginning of the conversation history
45
+ conversation_history.insert(0, {"role": "system", "content": SYSTEM_CONTENT})
46
 
47
+ # Create the chat session
48
+ chat_session = model.start_chat(
49
+ history=[
50
+ {"role": msg["role"], "content": msg["content"]} for msg in conversation_history
51
+ ]
52
+ )
 
 
 
 
 
 
 
 
 
53
 
54
+ # Send user input to the chat session
55
+ response = chat_session.send_message(user_input)
56
+ response_text = response.text
57
 
58
  # Add assistant response to conversation history
59
  conversation_history.append({"role": "assistant", "content": response_text})
 
64
  file("audio.mp3"), # Path to the local audio file
65
  0.333, # ttv_temperature
66
  0.333, # vc_temperature
67
+ 1, # duration_temperature
68
+ 1, # duration_length
69
  0, # denoise_ratio
70
  0, # random_seed
71
  api_name="/predict"
 
83
  description="Contact me if you want another character/voice<br>WhatsApp me: +92-332-4399819<br> Email me: aheedsajid@gmail.com<br><b>Donate something to increase GPU power</b><br>[Click here to Donate](https://nowpayments.io/donation/aheed)"
84
  )
85
 
86
+ iface.launch()