Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,13 +8,17 @@ import os
|
|
| 8 |
import speech_recognition as sr
|
| 9 |
from pydub import AudioSegment
|
| 10 |
import tempfile
|
|
|
|
| 11 |
|
| 12 |
# Configuration
|
| 13 |
NASA_API_URL = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY"
|
| 14 |
-
HF_MODEL_NAME = "all-MiniLM-L6-v2"
|
| 15 |
-
|
| 16 |
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_KEY")
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
# Language Configuration
|
| 19 |
LANGUAGE_CODES = {
|
| 20 |
'English': 'en-US',
|
|
@@ -25,13 +29,9 @@ LANGUAGE_CODES = {
|
|
| 25 |
'Arabic': 'ar-SA'
|
| 26 |
}
|
| 27 |
|
| 28 |
-
# Set Hugging Face API token in environment
|
| 29 |
-
os.environ["HUGGINGFACE_API_KEY"] = HUGGINGFACE_API_TOKEN
|
| 30 |
-
|
| 31 |
def speech_to_text(audio_file, language_code):
|
| 32 |
"""Convert uploaded audio file to text"""
|
| 33 |
recognizer = sr.Recognizer()
|
| 34 |
-
|
| 35 |
try:
|
| 36 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 37 |
tmp_file.write(audio_file.getvalue())
|
|
@@ -70,27 +70,27 @@ def load_knowledge_base():
|
|
| 70 |
except Exception as e:
|
| 71 |
return None
|
| 72 |
|
| 73 |
-
def
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
|
|
|
| 83 |
researcher = Agent(
|
| 84 |
role="Multilingual Space Analyst",
|
| 85 |
goal="Analyze and validate space information",
|
| 86 |
backstory="Expert in multilingual space data analysis with NASA mission experience.",
|
| 87 |
verbose=True,
|
| 88 |
-
llm=
|
| 89 |
-
llm_kwargs={
|
| 90 |
-
"temperature": 0.4,
|
| 91 |
-
"max_length": 512
|
| 92 |
-
},
|
| 93 |
-
memory=True
|
| 94 |
)
|
| 95 |
|
| 96 |
educator = Agent(
|
|
@@ -98,12 +98,7 @@ def setup_agents(language='en'):
|
|
| 98 |
goal=f"Explain complex concepts in {language} using simple terms",
|
| 99 |
backstory=f"Multilingual science communicator specializing in {language} explanations.",
|
| 100 |
verbose=True,
|
| 101 |
-
llm=
|
| 102 |
-
llm_kwargs={
|
| 103 |
-
"temperature": 0.5,
|
| 104 |
-
"max_length": 612
|
| 105 |
-
},
|
| 106 |
-
memory=True
|
| 107 |
)
|
| 108 |
|
| 109 |
return researcher, educator
|
|
|
|
| 8 |
import speech_recognition as sr
|
| 9 |
from pydub import AudioSegment
|
| 10 |
import tempfile
|
| 11 |
+
import litellm
|
| 12 |
|
| 13 |
# Configuration
|
| 14 |
NASA_API_URL = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY"
|
| 15 |
+
HF_MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
|
| 16 |
+
LLM_MODEL = "HuggingFaceH4/zephyr-7b-beta"
|
| 17 |
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_KEY")
|
| 18 |
|
| 19 |
+
# Set Hugging Face API token for litellm
|
| 20 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = HUGGINGFACE_API_TOKEN
|
| 21 |
+
|
| 22 |
# Language Configuration
|
| 23 |
LANGUAGE_CODES = {
|
| 24 |
'English': 'en-US',
|
|
|
|
| 29 |
'Arabic': 'ar-SA'
|
| 30 |
}
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
def speech_to_text(audio_file, language_code):
|
| 33 |
"""Convert uploaded audio file to text"""
|
| 34 |
recognizer = sr.Recognizer()
|
|
|
|
| 35 |
try:
|
| 36 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 37 |
tmp_file.write(audio_file.getvalue())
|
|
|
|
| 70 |
except Exception as e:
|
| 71 |
return None
|
| 72 |
|
| 73 |
+
def call_llm(prompt):
|
| 74 |
+
"""Correctly call Hugging Face models using litellm"""
|
| 75 |
+
try:
|
| 76 |
+
response = litellm.completion(
|
| 77 |
+
model="huggingface",
|
| 78 |
+
messages=[{"role": "user", "content": prompt}],
|
| 79 |
+
api_base="https://api-inference.huggingface.co/models",
|
| 80 |
+
api_key=HUGGINGFACE_API_TOKEN,
|
| 81 |
+
model_kwargs={"model": LLM_MODEL, "temperature": 0.4}
|
| 82 |
+
)
|
| 83 |
+
return response["choices"][0]["message"]["content"]
|
| 84 |
+
except Exception as e:
|
| 85 |
+
return f"LLM Error: {str(e)}"
|
| 86 |
|
| 87 |
+
def setup_agents(language='en'):
|
| 88 |
researcher = Agent(
|
| 89 |
role="Multilingual Space Analyst",
|
| 90 |
goal="Analyze and validate space information",
|
| 91 |
backstory="Expert in multilingual space data analysis with NASA mission experience.",
|
| 92 |
verbose=True,
|
| 93 |
+
llm=call_llm # ✅ Now correctly calls Hugging Face using litellm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
)
|
| 95 |
|
| 96 |
educator = Agent(
|
|
|
|
| 98 |
goal=f"Explain complex concepts in {language} using simple terms",
|
| 99 |
backstory=f"Multilingual science communicator specializing in {language} explanations.",
|
| 100 |
verbose=True,
|
| 101 |
+
llm=call_llm # ✅ Now correctly calls Hugging Face using litellm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
)
|
| 103 |
|
| 104 |
return researcher, educator
|