ghsambit commited on
Commit
71467d9
Β·
1 Parent(s): 5fdd0c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -53
app.py CHANGED
@@ -1,55 +1,40 @@
1
  import gradio as gr
2
- from together import Together
3
- from gtts import gTTS
4
- import tempfile
5
  import os
6
-
7
- # Get your Together API key from Hugging Face secret
8
- TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY")
9
-
10
- # Initialize Together client
11
- client = Together(api_key=TOGETHER_API_KEY)
12
-
13
- # Text-to-speech with gTTS
14
- def speak_text(text):
15
- tts = gTTS(text)
16
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
17
- tts.save(fp.name)
18
- return fp.name
19
-
20
- # Main chat function
21
- def respond(message, history):
22
- history = history or []
23
- prompt = ""
24
- for user_msg, bot_msg in history:
25
- prompt += f"<|user|> {user_msg}\n<|assistant|> {bot_msg}\n"
26
- prompt += f"<|user|> {message}\n<|assistant|>"
27
-
28
- # Call Together API
29
- response = client.chat.completions.create(
30
- model="meta-llama/Llama-3-8B-Instruct",
31
- messages=[{"role": "user", "content": prompt}],
32
- max_tokens=512,
33
- temperature=0.7,
34
- top_p=0.95
35
- )
36
-
37
- reply = response.choices[0].message.content.strip()
38
- history.append((message, reply))
39
- audio_path = speak_text(reply)
40
- return history, history, audio_path
41
-
42
- # Gradio UI
43
- with gr.Blocks() as demo:
44
- gr.Markdown("## Sambit AI πŸ€– β€” Powered by Together & LLaMA 3")
45
-
46
- chatbot = gr.Chatbot(label="Chat with AI")
47
- msg = gr.Textbox(label="Type your message here")
48
- audio_output = gr.Audio(label="AI Voice", interactive=False)
49
- clear = gr.Button("Clear Chat")
50
- state = gr.State([])
51
-
52
- msg.submit(respond, [msg, state], [chatbot, state, audio_output])
53
- clear.click(lambda: ([], [], None), None, [chatbot, state, audio_output])
54
-
55
- demo.launch()
 
1
  import gradio as gr
 
 
 
2
  import os
3
+ import requests
4
+
5
+ # βœ… Load API key from Hugging Face Secrets (set in Settings β†’ Secrets)
6
+ API_KEY = os.getenv("TOGETHER_API_KEY")
7
+ MODEL_NAME = "mistralai/Mixtral-8x7B-Instruct-v0.1"
8
+
9
+ def ask_ai(message):
10
+ if not API_KEY:
11
+ return "❌ API key not found. Please set it in Settings β†’ Secrets."
12
+
13
+ url = "https://api.together.xyz/v1/chat/completions"
14
+ headers = {
15
+ "Authorization": f"Bearer {API_KEY}",
16
+ "Content-Type": "application/json"
17
+ }
18
+ data = {
19
+ "model": MODEL_NAME,
20
+ "messages": [
21
+ {"role": "system", "content": "You are Sambit AI, a helpful assistant."},
22
+ {"role": "user", "content": message}
23
+ ]
24
+ }
25
+
26
+ try:
27
+ response = requests.post(url, headers=headers, json=data)
28
+ response.raise_for_status()
29
+ result = response.json()
30
+ reply = result['choices'][0]['message']['content']
31
+ return reply
32
+ except Exception as e:
33
+ return f"❌ Error: {str(e)}"
34
+
35
+ # βœ… Gradio UI
36
+ chat_interface = gr.ChatInterface(fn=ask_ai, title="Sambit AI πŸ€– β€” Powered by Together & LLaMA 3")
37
+
38
+ # βœ… Launch the app
39
+ if __name__ == "__main__":
40
+ chat_interface.launch()