pushpinder06 commited on
Commit
e355f70
·
verified ·
1 Parent(s): 9c1234b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from google.generativeai import GenerativeModel, configure
3
+
4
+ # ✅ Load API key from environment variable
5
+ GOOGLE_API_KEY = os.getenv("GEMINI_API_KEY")
6
+ if not GOOGLE_API_KEY:
7
+ raise ValueError("❌ Missing API Key! Please set GEMINI_API_KEY as an environment variable.")
8
+
9
+ # ✅ Configure Gemini securely
10
+ configure(api_key=GOOGLE_API_KEY)
11
+ gemini_model = GenerativeModel("models/gemini-1.5-flash")
12
+
13
+ def get_gemini_response(query):
14
+ try:
15
+ response = gemini_model.generate_content(f"ਪੰਜਾਬੀ ਵਿੱਚ ਜਵਾਬ ਦਿਓ: {query}")
16
+ return response.text.replace('*', '')
17
+ except Exception as e:
18
+ return f"❌ Gemini ਤਰਫੋਂ ਗਲਤੀ: {str(e)}"
19
+
20
+ def text_to_speech(text, lang='pa'):
21
+ tts = gTTS(text=text, lang=lang)
22
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
23
+ tts.save(temp_file.name)
24
+ return temp_file.name
25
+
26
+ # ---------------------------
27
+ # Combined Function
28
+ # ---------------------------
29
+ def handle_voice_query(audio_file):
30
+ query = transcribe_audio(audio_file)
31
+ response = get_gemini_response(query)
32
+ audio_path = text_to_speech(response)
33
+ return query, response, audio_path
34
+
35
+ with gr.Blocks() as demo:
36
+ gr.Markdown("# 🌾 **AgroVision: ਪੰਜਾਬੀ ਵੈਚਲਣ ਸਹਾਇਕ**")
37
+
38
+ with gr.TabItem("🗣️ ਆਵਾਜ਼ ਰਾਹੀਂ ਪੁੱਛੋ"):
39
+ gr.Markdown("### ਆਪਣਾ ਸਵਾਲ ਆਵਾਜ਼ ਰਾਹੀਂ ਪੁੱਛੋ (ਪੰਜਾਬੀ ਵਿੱਚ)")
40
+ audio_input = gr.Audio(type="filepath", label="🎤 ਸਵਾਲ ਬੋਲੋ")
41
+ query_text = gr.Textbox(label="🔍 ਬੋਲਿਆ ਗਿਆ ਸਵਾਲ")
42
+ gemini_response = gr.Textbox(label="📜 Gemini ਜਵਾਬ")
43
+ audio_output = gr.Audio(label="🔊 ਆਵਾਜ਼ੀ ਜਵਾਬ")
44
+ submit_btn = gr.Button("➡️ ਜਵਾਬ ਲਵੋ")
45
+ submit_btn.click(fn=handle_voice_query,
46
+ inputs=[audio_input],
47
+ outputs=[query_text, gemini_response, audio_output])
48
+
49
+ demo.launch()