anasfsd123 commited on
Commit
c50d138
·
verified ·
1 Parent(s): ce88f6d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +174 -0
app.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from crewai import Agent, Task, Crew
3
+ from langchain_community.embeddings import HuggingFaceEmbeddings
4
+ from langchain_community.vectorstores import FAISS
5
+ from langchain_community.document_loaders import WebBaseLoader
6
+ import requests
7
+ 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
+ LLM_REPO = "HuggingFaceH4/zephyr-7b-beta"
16
+ HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_KEY")
17
+
18
+ # Language Configuration
19
+ LANGUAGE_CODES = {
20
+ 'English': 'en-US',
21
+ 'Spanish': 'es-ES',
22
+ 'French': 'fr-FR',
23
+ 'German': 'de-DE',
24
+ 'Chinese': 'zh-CN',
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
+ # Save uploaded file to temporary location
37
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") 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"
45
+ audio.export(wav_path, format="wav")
46
+ audio_path = wav_path
47
+
48
+ with sr.AudioFile(audio_path) as source:
49
+ audio_data = recognizer.record(source)
50
+ return recognizer.recognize_google(audio_data, language=language_code)
51
+
52
+ except Exception as e:
53
+ st.error(f"Audio processing error: {str(e)}")
54
+ return ""
55
+ finally:
56
+ if os.path.exists(audio_path):
57
+ os.remove(audio_path)
58
+
59
+ def get_nasa_data():
60
+ try:
61
+ return requests.get(NASA_API_URL, timeout=10).json()
62
+ except Exception as e:
63
+ return {"error": f"NASA API Error: {str(e)}"}
64
+
65
+ def load_knowledge_base():
66
+ try:
67
+ loader = WebBaseLoader(["https://mars.nasa.gov/news/"])
68
+ docs = loader.load()[:3]
69
+ embeddings = HuggingFaceEmbeddings(model_name=HF_MODEL_NAME)
70
+ return FAISS.from_documents(docs, embeddings)
71
+ except Exception as e:
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=LLM_REPO,
90
+ llm_kwargs={
91
+ "temperature": 0.4,
92
+ "max_length": 512
93
+ },
94
+ memory=True
95
+ )
96
+
97
+ educator = Agent(
98
+ role="Bilingual Science Educator",
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=LLM_REPO,
103
+ llm_kwargs={
104
+ "temperature": 0.5,
105
+ "max_length": 612
106
+ },
107
+ memory=True
108
+ )
109
+
110
+ return researcher, educator
111
+
112
+ def process_question(question, target_lang='en'):
113
+ try:
114
+ nasa_data = get_nasa_data()
115
+ vector_store = load_knowledge_base()
116
+ researcher, educator = setup_agents(target_lang)
117
+
118
+ research_task = Task(
119
+ description=f"""Research: {question}
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(
128
+ description=f"Explain in {target_lang} using simple terms and analogies",
129
+ agent=educator,
130
+ expected_output="2-paragraph answer in requested language",
131
+ context=[research_task]
132
+ )
133
+
134
+ crew = Crew(
135
+ agents=[researcher, educator],
136
+ tasks=[research_task, explain_task],
137
+ verbose=True
138
+ )
139
+
140
+ return crew.kickoff()
141
+ except Exception as e:
142
+ return f"Error: {str(e)}"
143
+
144
+ # Streamlit Interface
145
+ st.title("🚀 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] # Extract base language code
151
+
152
+ # Input Method
153
+ input_method = st.radio("Input Method", ["Text", "Audio File"])
154
+
155
+ question = ""
156
+ 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])
164
+ if question:
165
+ st.text_area("Transcribed Text", value=question, height=100)
166
+
167
+ if question:
168
+ with st.spinner("Analyzing with AI agents..."):
169
+ answer = process_question(question, lang_code)
170
+ st.markdown(f"### 🌍 Answer ({selected_lang}):")
171
+ st.markdown(answer)
172
+
173
+ st.markdown("---")
174
+ st.markdown("*Powered by NASA API & Open Source AI*")