mgokg commited on
Commit
abae114
·
verified ·
1 Parent(s): 320e043

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -69
app.py CHANGED
@@ -4,7 +4,7 @@ import os
4
  import json
5
  import google.generativeai as genai
6
  from bs4 import BeautifulSoup
7
- from groq import Groq
8
  # Load environment variables
9
  genai.configure(api_key=os.environ["geminiapikey"])
10
  read_key = os.environ.get('HF_TOKEN', None)
@@ -21,13 +21,13 @@ custom_css = """
21
  }
22
  """
23
 
24
- api_key = os.getenv('groq')
25
  google_api_key = os.getenv('google_search')
26
  if api_key is None:
27
  raise ValueError("groq_whisper environment variable is not set")
28
 
29
  # Initialize the Groq client
30
- client = Groq(api_key=api_key)
31
 
32
  def perform_search(prompt):
33
  if prompt.strip() == '':
@@ -39,26 +39,20 @@ def perform_search(prompt):
39
  try:
40
  # HTTP GET-Anfrage an die Google Custom Search API
41
  response = requests.get(url)
42
- response.raise_for_status() # Wirft eine Exception, wenn die Anfrage fehlschlägt
43
- #soup = BeautifulSoup(response.content, 'html.parser')
44
- #response_text = soup.find('body')
45
- #return response_text.text
46
  # JSON-Antwort parsen
47
- data = response.json()
48
-
49
  # Extrahiere die Suchergebnisse
50
- items = data.get('items', [])
51
-
52
  results = [item['snippet'] for item in items]
53
  #return results[0]
54
  # Kombiniere die Ergebnisse zu einem String
55
  result_text = '\n'.join(results)
56
  #return results[0]
57
  # Formuliere die Antwort
58
- search_query = f"{prompt} antworte kurz und knapp. antworte auf deutsch. du findest die antwort hier: {result_text}"
59
  #result = predict(search_query)
60
  #return result
61
- return search_query
62
 
63
  except requests.exceptions.RequestException as e:
64
  print(f"An error occurred: {e}")
@@ -85,42 +79,6 @@ def predict(prompt):
85
  response = chat_session.send_message(f"{prompt}\n antworte immer auf deutsch")
86
  response_value = response.candidates[0].content.parts[0].text
87
  return response_value
88
-
89
- #very simple (and extremly fast) websearch
90
- def websearch(search_term):
91
- headers = {
92
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
93
- }
94
- url = f"https://www.google.com/search?q={search_term}"
95
- response = requests.get(url, headers=headers)
96
- soup = BeautifulSoup(response.content, 'html.parser')
97
- response_text = soup.find('body')
98
- prompt = f"{search_term}\n use this result from a google search to answer the question \n {response_text.text}"
99
- result = predict(prompt)
100
- return result
101
-
102
-
103
- def process_audio(file_path):
104
- try:
105
- # Open the audio file
106
- with open(file_path, "rb") as file:
107
- # Create a translation of the audio file
108
- translation = client.audio.transcriptions.create(
109
- file=(os.path.basename(file_path), file.read()), # Correct passing of filename
110
- model="whisper-large-v3-turbo", # Required model to use for translation
111
- prompt="transcribe", # Optional
112
- language="de", # Optional
113
- response_format="json", # Optional
114
- temperature=0.0 # Optional
115
- )
116
- # Return the translation text
117
- suche = websearch(translation.text)
118
- result = predict(suche)
119
- return result
120
- return translation.text
121
- except Exception as e:
122
- return f"An error occurred: {str(e)}"
123
-
124
 
125
  # Create the Gradio interface
126
  with gr.Blocks(css=custom_css) as demo:
@@ -139,23 +97,3 @@ with gr.Blocks(css=custom_css) as demo:
139
  # Launch the Gradio application
140
  demo.launch()
141
 
142
-
143
-
144
-
145
- """
146
- with gr.Blocks() as speech:
147
- with gr.Row():
148
- sr_outputs = gr.Textbox(label="Antwort")
149
- with gr.Row():
150
- sr_inputs = gr.Microphone(type="filepath")
151
- sr_inputs.change(process_audio, inputs=sr_inputs, outputs=sr_outputs)
152
-
153
- speech.launch()
154
-
155
-
156
-
157
-
158
-
159
- """
160
-
161
-
 
4
  import json
5
  import google.generativeai as genai
6
  from bs4 import BeautifulSoup
7
+ #from groq import Groq
8
  # Load environment variables
9
  genai.configure(api_key=os.environ["geminiapikey"])
10
  read_key = os.environ.get('HF_TOKEN', None)
 
21
  }
22
  """
23
 
24
+ #api_key = os.getenv('groq')
25
  google_api_key = os.getenv('google_search')
26
  if api_key is None:
27
  raise ValueError("groq_whisper environment variable is not set")
28
 
29
  # Initialize the Groq client
30
+ #client = Groq(api_key=api_key)
31
 
32
  def perform_search(prompt):
33
  if prompt.strip() == '':
 
39
  try:
40
  # HTTP GET-Anfrage an die Google Custom Search API
41
  response = requests.get(url)
 
 
 
 
42
  # JSON-Antwort parsen
43
+ data = response.json()
 
44
  # Extrahiere die Suchergebnisse
45
+ items = data.get('items', [])
 
46
  results = [item['snippet'] for item in items]
47
  #return results[0]
48
  # Kombiniere die Ergebnisse zu einem String
49
  result_text = '\n'.join(results)
50
  #return results[0]
51
  # Formuliere die Antwort
52
+ #search_query = f"{prompt} antworte kurz und knapp. antworte auf deutsch. du findest die antwort hier: {result_text}"
53
  #result = predict(search_query)
54
  #return result
55
+ return result_text
56
 
57
  except requests.exceptions.RequestException as e:
58
  print(f"An error occurred: {e}")
 
79
  response = chat_session.send_message(f"{prompt}\n antworte immer auf deutsch")
80
  response_value = response.candidates[0].content.parts[0].text
81
  return response_value
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
  # Create the Gradio interface
84
  with gr.Blocks(css=custom_css) as demo:
 
97
  # Launch the Gradio application
98
  demo.launch()
99