anasfsd123 commited on
Commit
8f6aefe
·
verified ·
1 Parent(s): 64fbf2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -12
app.py CHANGED
@@ -12,7 +12,6 @@ import tempfile
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
- LLM_REPO = "HuggingFaceH4/zephyr-7b-beta"
16
  HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_KEY")
17
 
18
  # Set Hugging Face API token
@@ -31,11 +30,14 @@ LANGUAGE_CODES = {
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,15 +71,22 @@ def load_knowledge_base():
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,10 +99,7 @@ def setup_agents(language='en'):
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
@@ -104,6 +110,7 @@ def setup_agents(language='en'):
104
  return researcher, educator
105
 
106
  def process_question(question, target_lang='en'):
 
107
  try:
108
  nasa_data = get_nasa_data()
109
  vector_store = load_knowledge_base()
@@ -114,7 +121,8 @@ def process_question(question, target_lang='en'):
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,12 +143,14 @@ def process_question(question, target_lang='en'):
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,6 +158,7 @@ if input_method == "Text":
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])
 
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
  HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_KEY")
16
 
17
  # Set Hugging Face API token
 
30
  def speech_to_text(audio_file, language_code):
31
  """Convert uploaded audio file to text"""
32
  recognizer = sr.Recognizer()
33
+
34
  try:
35
+ # Save uploaded file to temporary location
36
  with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
37
  tmp_file.write(audio_file.getvalue())
38
  audio_path = tmp_file.name
39
 
40
+ # Convert to WAV if necessary
41
  if not audio_path.endswith('.wav'):
42
  audio = AudioSegment.from_file(audio_path)
43
  wav_path = audio_path + ".wav"
 
71
  return None
72
 
73
  def setup_agents(language='en'):
74
+ """Setup CrewAI agents with the correct LLM provider"""
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={"model": "HuggingFaceH4/zephyr-7b-beta", "provider": "huggingface"},
 
 
 
90
  llm_kwargs={
91
  "temperature": 0.4,
92
  "max_length": 512
 
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={"model": "HuggingFaceH4/zephyr-7b-beta", "provider": "huggingface"},
 
 
 
103
  llm_kwargs={
104
  "temperature": 0.5,
105
  "max_length": 612
 
110
  return researcher, educator
111
 
112
  def process_question(question, target_lang='en'):
113
+ """Process the user's question using AI agents"""
114
  try:
115
  nasa_data = get_nasa_data()
116
  vector_store = load_knowledge_base()
 
121
  NASA Context: {nasa_data.get('explanation', '')}
122
  Language: {target_lang}""",
123
  agent=researcher,
124
+ expected_output="3 verified technical points",
125
+ output_file="research.md"
126
  )
127
 
128
  explain_task = Task(
 
143
  return f"Error: {str(e)}"
144
 
145
  # Streamlit Interface
146
+ st.title("🚀COSMOLAB (Multilingual Space Agent)")
147
  st.markdown("### Ask space questions in any language!")
148
 
149
+ # Single language selection for both input and output
150
  selected_lang = st.selectbox("Select Language", list(LANGUAGE_CODES.keys()))
151
+ lang_code = LANGUAGE_CODES[selected_lang].split('-')[0] # Extract base language code
152
 
153
+ # Input Method
154
  input_method = st.radio("Input Method", ["Text", "Audio File"])
155
 
156
  question = ""
 
158
  question = st.text_input(f"Your space question in {selected_lang}:", "")
159
  else:
160
  audio_file = st.file_uploader("Upload audio file", type=["wav", "mp3", "ogg"])
161
+
162
  if audio_file is not None:
163
  with st.spinner("Processing audio..."):
164
  question = speech_to_text(audio_file, LANGUAGE_CODES[selected_lang])