Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
%%writefile app.py
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM # Fixed import name
|
4 |
+
import gradio as gr
|
5 |
+
import warnings
|
6 |
+
warnings.filterwarnings('ignore')
|
7 |
+
|
8 |
+
class SpaceBot:
|
9 |
+
def __init__(self):
|
10 |
+
# Initialize model and tokenizer
|
11 |
+
print("Loading model and tokenizer...")
|
12 |
+
self.model_name = "google/flan-t5-base" # Using a more stable model
|
13 |
+
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
|
14 |
+
self.model = AutoModelForSeq2SeqLM.from_pretrained(self.model_name)
|
15 |
+
|
16 |
+
# Space facts database
|
17 |
+
self.space_facts = {
|
18 |
+
"apollo": [
|
19 |
+
"The Apollo program ran from 1961 to 1972.",
|
20 |
+
"Apollo 11 was the first mission to land humans on the Moon.",
|
21 |
+
"Neil Armstrong and Buzz Aldrin walked on the Moon on July 20, 1969.",
|
22 |
+
"The Apollo program included 17 space missions in total."
|
23 |
+
],
|
24 |
+
"iss": [
|
25 |
+
"The International Space Station orbits Earth at about 400 kilometers above the surface.",
|
26 |
+
"The ISS has been continuously occupied since November 2000.",
|
27 |
+
"The ISS travels at approximately 7.66 kilometers per second.",
|
28 |
+
"The space station is about the size of a football field."
|
29 |
+
],
|
30 |
+
"mars": [
|
31 |
+
"Mars is often called the Red Planet due to its reddish appearance.",
|
32 |
+
"NASA's Perseverance rover landed on Mars in 2021.",
|
33 |
+
"Mars has two small moons: Phobos and Deimos.",
|
34 |
+
"The first successful Mars landing was by NASA's Viking 1 in 1976."
|
35 |
+
],
|
36 |
+
"telescope": [
|
37 |
+
"The Hubble Space Telescope was launched in 1990.",
|
38 |
+
"The James Webb Space Telescope is the largest space telescope ever built.",
|
39 |
+
"Space telescopes can see more clearly than ground-based telescopes.",
|
40 |
+
"The Hubble has made over 1.5 million observations."
|
41 |
+
],
|
42 |
+
"general": [
|
43 |
+
"The first human in space was Yuri Gagarin in 1961.",
|
44 |
+
"SpaceX achieved the first successful landing of a reusable rocket in 2015.",
|
45 |
+
"There are currently over 5,000 satellites orbiting Earth.",
|
46 |
+
"Light takes about 8 minutes to travel from the Sun to Earth."
|
47 |
+
]
|
48 |
+
}
|
49 |
+
|
50 |
+
def get_relevant_facts(self, query):
|
51 |
+
"""Get relevant facts based on keywords in the query"""
|
52 |
+
query = query.lower()
|
53 |
+
relevant_facts = []
|
54 |
+
|
55 |
+
# Check each category for relevant facts
|
56 |
+
for category, facts in self.space_facts.items():
|
57 |
+
if category in query or any(keyword in query for keyword in category.split()):
|
58 |
+
relevant_facts.extend(facts)
|
59 |
+
|
60 |
+
# If no specific facts found, return general facts
|
61 |
+
if not relevant_facts:
|
62 |
+
relevant_facts = self.space_facts["general"]
|
63 |
+
|
64 |
+
return " ".join(relevant_facts[:2]) # Return two relevant facts
|
65 |
+
|
66 |
+
def generate_response(self, query):
|
67 |
+
try:
|
68 |
+
# Get relevant facts as context
|
69 |
+
context = self.get_relevant_facts(query)
|
70 |
+
|
71 |
+
# Prepare input text
|
72 |
+
input_text = f"Using this context: {context}\nAnswer this question: {query}"
|
73 |
+
|
74 |
+
# Tokenize input
|
75 |
+
inputs = self.tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
|
76 |
+
|
77 |
+
# Generate response
|
78 |
+
outputs = self.model.generate(
|
79 |
+
inputs.input_ids,
|
80 |
+
max_length=150,
|
81 |
+
min_length=30,
|
82 |
+
num_beams=4,
|
83 |
+
temperature=0.7,
|
84 |
+
no_repeat_ngram_size=2
|
85 |
+
)
|
86 |
+
|
87 |
+
# Decode response
|
88 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
89 |
+
|
90 |
+
return response
|
91 |
+
except Exception as e:
|
92 |
+
print(f"Error generating response: {str(e)}")
|
93 |
+
return "I apologize, but I couldn't generate a proper response. Please try asking your question in a different way."
|
94 |
+
|
95 |
+
def chat_with_bot(message, history):
|
96 |
+
"""Handle chat interactions"""
|
97 |
+
try:
|
98 |
+
response = bot.generate_response(message)
|
99 |
+
return [(response, None)]
|
100 |
+
except Exception as e:
|
101 |
+
print(f"Error in chat interface: {str(e)}")
|
102 |
+
return [("I encountered an error. Please try asking your question again.", None)]
|
103 |
+
|
104 |
+
print("Initializing SpaceBot...")
|
105 |
+
# Initialize the bot
|
106 |
+
bot = SpaceBot()
|
107 |
+
print("SpaceBot initialization complete!")
|
108 |
+
|
109 |
+
# Create Gradio interface
|
110 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
111 |
+
gr.Markdown("""
|
112 |
+
# 🚀 Space Explorer Chat
|
113 |
+
Ask me anything about space exploration and astronomy!
|
114 |
+
""")
|
115 |
+
|
116 |
+
chatbot = gr.ChatInterface(
|
117 |
+
chat_with_bot,
|
118 |
+
chatbot=gr.Chatbot(height=400),
|
119 |
+
textbox=gr.Textbox(
|
120 |
+
placeholder="Ask about space missions, planets, or astronomy...",
|
121 |
+
container=False
|
122 |
+
),
|
123 |
+
title="Space Explorer",
|
124 |
+
description="Your guide to space exploration and astronomy",
|
125 |
+
theme="soft",
|
126 |
+
examples=[
|
127 |
+
"What was the Apollo program?",
|
128 |
+
"Tell me about the International Space Station",
|
129 |
+
"What do you know about Mars exploration?",
|
130 |
+
"Tell me about space telescopes",
|
131 |
+
"What are some interesting space facts?"
|
132 |
+
],
|
133 |
+
)
|
134 |
+
|
135 |
+
gr.Markdown("""
|
136 |
+
### Topics I can help with:
|
137 |
+
- Space missions and history
|
138 |
+
- Planets and astronomy
|
139 |
+
- Space stations and satellites
|
140 |
+
- Space telescopes
|
141 |
+
- General space facts
|
142 |
+
""")
|
143 |
+
|
144 |
+
# Launch the interface
|
145 |
+
demo.launch(share=True)
|