Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,7 +15,7 @@ HF_MODEL_NAME = "all-MiniLM-L6-v2"
|
|
| 15 |
LLM_REPO = "HuggingFaceH4/zephyr-7b-beta"
|
| 16 |
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_KEY")
|
| 17 |
|
| 18 |
-
# Set Hugging Face API token
|
| 19 |
os.environ["HUGGINGFACEHUB_API_TOKEN"] = HUGGINGFACE_API_TOKEN
|
| 20 |
|
| 21 |
# Language Configuration
|
|
@@ -31,14 +31,11 @@ LANGUAGE_CODES = {
|
|
| 31 |
def speech_to_text(audio_file, language_code):
|
| 32 |
"""Convert uploaded audio file to text"""
|
| 33 |
recognizer = sr.Recognizer()
|
| 34 |
-
|
| 35 |
try:
|
| 36 |
-
# Save uploaded file to temporary location
|
| 37 |
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
| 38 |
tmp_file.write(audio_file.getvalue())
|
| 39 |
audio_path = tmp_file.name
|
| 40 |
|
| 41 |
-
# Convert to WAV if necessary
|
| 42 |
if not audio_path.endswith('.wav'):
|
| 43 |
audio = AudioSegment.from_file(audio_path)
|
| 44 |
wav_path = audio_path + ".wav"
|
|
@@ -72,21 +69,15 @@ def load_knowledge_base():
|
|
| 72 |
return None
|
| 73 |
|
| 74 |
def setup_agents(language='en'):
|
| 75 |
-
prompts = {
|
| 76 |
-
'en': "Explain space concepts clearly in English",
|
| 77 |
-
'es': "Explica conceptos espaciales en español",
|
| 78 |
-
'fr': "Expliquez les concepts spatiaux en français",
|
| 79 |
-
'de': "Erklären Sie Raumfahrtkonzepte auf Deutsch",
|
| 80 |
-
'zh': "用中文清楚解释空间概念",
|
| 81 |
-
'ar': "اشرح مفاهيم الفضاء باللغة العربية"
|
| 82 |
-
}
|
| 83 |
-
|
| 84 |
researcher = Agent(
|
| 85 |
role="Multilingual Space Analyst",
|
| 86 |
goal="Analyze and validate space information",
|
| 87 |
backstory="Expert in multilingual space data analysis with NASA mission experience.",
|
| 88 |
verbose=True,
|
| 89 |
-
llm=
|
|
|
|
|
|
|
|
|
|
| 90 |
llm_kwargs={
|
| 91 |
"temperature": 0.4,
|
| 92 |
"max_length": 512
|
|
@@ -99,7 +90,10 @@ def setup_agents(language='en'):
|
|
| 99 |
goal=f"Explain complex concepts in {language} using simple terms",
|
| 100 |
backstory=f"Multilingual science communicator specializing in {language} explanations.",
|
| 101 |
verbose=True,
|
| 102 |
-
llm=
|
|
|
|
|
|
|
|
|
|
| 103 |
llm_kwargs={
|
| 104 |
"temperature": 0.5,
|
| 105 |
"max_length": 612
|
|
@@ -120,8 +114,7 @@ def process_question(question, target_lang='en'):
|
|
| 120 |
NASA Context: {nasa_data.get('explanation', '')}
|
| 121 |
Language: {target_lang}""",
|
| 122 |
agent=researcher,
|
| 123 |
-
expected_output="3 verified technical points"
|
| 124 |
-
output_file="research.md"
|
| 125 |
)
|
| 126 |
|
| 127 |
explain_task = Task(
|
|
@@ -142,14 +135,12 @@ def process_question(question, target_lang='en'):
|
|
| 142 |
return f"Error: {str(e)}"
|
| 143 |
|
| 144 |
# Streamlit Interface
|
| 145 |
-
st.title("🚀COSMOLAB (Multilingual Space Agent)")
|
| 146 |
st.markdown("### Ask space questions in any language!")
|
| 147 |
|
| 148 |
-
# Single language selection for both input and output
|
| 149 |
selected_lang = st.selectbox("Select Language", list(LANGUAGE_CODES.keys()))
|
| 150 |
-
lang_code = LANGUAGE_CODES[selected_lang].split('-')[0]
|
| 151 |
|
| 152 |
-
# Input Method
|
| 153 |
input_method = st.radio("Input Method", ["Text", "Audio File"])
|
| 154 |
|
| 155 |
question = ""
|
|
@@ -157,7 +148,6 @@ if input_method == "Text":
|
|
| 157 |
question = st.text_input(f"Your space question in {selected_lang}:", "")
|
| 158 |
else:
|
| 159 |
audio_file = st.file_uploader("Upload audio file", type=["wav", "mp3", "ogg"])
|
| 160 |
-
|
| 161 |
if audio_file is not None:
|
| 162 |
with st.spinner("Processing audio..."):
|
| 163 |
question = speech_to_text(audio_file, LANGUAGE_CODES[selected_lang])
|
|
|
|
| 15 |
LLM_REPO = "HuggingFaceH4/zephyr-7b-beta"
|
| 16 |
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_KEY")
|
| 17 |
|
| 18 |
+
# Set Hugging Face API token
|
| 19 |
os.environ["HUGGINGFACEHUB_API_TOKEN"] = HUGGINGFACE_API_TOKEN
|
| 20 |
|
| 21 |
# Language Configuration
|
|
|
|
| 31 |
def speech_to_text(audio_file, language_code):
|
| 32 |
"""Convert uploaded audio file to text"""
|
| 33 |
recognizer = sr.Recognizer()
|
|
|
|
| 34 |
try:
|
|
|
|
| 35 |
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
| 36 |
tmp_file.write(audio_file.getvalue())
|
| 37 |
audio_path = tmp_file.name
|
| 38 |
|
|
|
|
| 39 |
if not audio_path.endswith('.wav'):
|
| 40 |
audio = AudioSegment.from_file(audio_path)
|
| 41 |
wav_path = audio_path + ".wav"
|
|
|
|
| 69 |
return None
|
| 70 |
|
| 71 |
def setup_agents(language='en'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
researcher = Agent(
|
| 73 |
role="Multilingual Space Analyst",
|
| 74 |
goal="Analyze and validate space information",
|
| 75 |
backstory="Expert in multilingual space data analysis with NASA mission experience.",
|
| 76 |
verbose=True,
|
| 77 |
+
llm={
|
| 78 |
+
"model": "HuggingFaceH4/zephyr-7b-beta",
|
| 79 |
+
"provider": "huggingface"
|
| 80 |
+
},
|
| 81 |
llm_kwargs={
|
| 82 |
"temperature": 0.4,
|
| 83 |
"max_length": 512
|
|
|
|
| 90 |
goal=f"Explain complex concepts in {language} using simple terms",
|
| 91 |
backstory=f"Multilingual science communicator specializing in {language} explanations.",
|
| 92 |
verbose=True,
|
| 93 |
+
llm={
|
| 94 |
+
"model": "HuggingFaceH4/zephyr-7b-beta",
|
| 95 |
+
"provider": "huggingface"
|
| 96 |
+
},
|
| 97 |
llm_kwargs={
|
| 98 |
"temperature": 0.5,
|
| 99 |
"max_length": 612
|
|
|
|
| 114 |
NASA Context: {nasa_data.get('explanation', '')}
|
| 115 |
Language: {target_lang}""",
|
| 116 |
agent=researcher,
|
| 117 |
+
expected_output="3 verified technical points"
|
|
|
|
| 118 |
)
|
| 119 |
|
| 120 |
explain_task = Task(
|
|
|
|
| 135 |
return f"Error: {str(e)}"
|
| 136 |
|
| 137 |
# Streamlit Interface
|
| 138 |
+
st.title("🚀 COSMOLAB (Multilingual Space Agent)")
|
| 139 |
st.markdown("### Ask space questions in any language!")
|
| 140 |
|
|
|
|
| 141 |
selected_lang = st.selectbox("Select Language", list(LANGUAGE_CODES.keys()))
|
| 142 |
+
lang_code = LANGUAGE_CODES[selected_lang].split('-')[0]
|
| 143 |
|
|
|
|
| 144 |
input_method = st.radio("Input Method", ["Text", "Audio File"])
|
| 145 |
|
| 146 |
question = ""
|
|
|
|
| 148 |
question = st.text_input(f"Your space question in {selected_lang}:", "")
|
| 149 |
else:
|
| 150 |
audio_file = st.file_uploader("Upload audio file", type=["wav", "mp3", "ogg"])
|
|
|
|
| 151 |
if audio_file is not None:
|
| 152 |
with st.spinner("Processing audio..."):
|
| 153 |
question = speech_to_text(audio_file, LANGUAGE_CODES[selected_lang])
|