Spaces:
Runtime error
Runtime error
halimbahae
commited on
Commit
β’
47a38f9
1
Parent(s):
c9ab602
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,9 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
3 |
-
from scrapegraphai.graphs import SmartScraperGraph
|
4 |
-
import torch
|
5 |
|
6 |
# Page config
|
7 |
st.set_page_config(
|
8 |
-
page_title="Zephyr Chat
|
9 |
page_icon="π€",
|
10 |
layout="wide"
|
11 |
)
|
@@ -14,98 +12,56 @@ st.set_page_config(
|
|
14 |
if "messages" not in st.session_state:
|
15 |
st.session_state.messages = []
|
16 |
|
17 |
-
|
18 |
-
st.session_state.scrape_results = None
|
19 |
-
|
20 |
-
# Load Zephyr model
|
21 |
@st.cache_resource
|
22 |
def load_model():
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
torch_dtype=torch.float16,
|
28 |
-
device_map="auto",
|
29 |
-
model_kwargs={"load_in_8bit": True} # Use 8-bit quantization to reduce memory usage
|
30 |
-
)
|
31 |
-
except Exception as e:
|
32 |
-
st.error(f"Error loading model: {str(e)}")
|
33 |
-
return None
|
34 |
-
|
35 |
-
# Initialize the model
|
36 |
-
model = load_model()
|
37 |
-
|
38 |
-
# Sidebar for web scraping
|
39 |
-
with st.sidebar:
|
40 |
-
st.title("Web Scraping")
|
41 |
-
url = st.text_input("Enter URL to scrape")
|
42 |
-
scrape_prompt = st.text_input("What information do you want to extract?")
|
43 |
-
|
44 |
-
if st.button("Scrape"):
|
45 |
-
try:
|
46 |
-
# Configure scraper
|
47 |
-
graph_config = {
|
48 |
-
"llm": {
|
49 |
-
"model": "HuggingFaceH4/zephyr-7b-beta",
|
50 |
-
"temperature": 0.7,
|
51 |
-
},
|
52 |
-
"verbose": True
|
53 |
-
}
|
54 |
-
|
55 |
-
# Create scraper instance
|
56 |
-
scraper = SmartScraperGraph(
|
57 |
-
prompt=scrape_prompt,
|
58 |
-
source=url,
|
59 |
-
config=graph_config
|
60 |
-
)
|
61 |
-
|
62 |
-
# Run scraping
|
63 |
-
st.session_state.scrape_results = scraper.run()
|
64 |
-
st.success("Scraping completed!")
|
65 |
-
|
66 |
-
except Exception as e:
|
67 |
-
st.error(f"Error during scraping: {str(e)}")
|
68 |
|
69 |
# Main chat interface
|
70 |
st.title("Zephyr Chatbot π€")
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
st.subheader("Scraped Information")
|
75 |
-
st.json(st.session_state.scrape_results)
|
76 |
|
77 |
-
# Display chat messages
|
78 |
-
for message in st.session_state.messages:
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
-
# Chat input
|
83 |
-
if prompt := st.chat_input("What's on your mind?"):
|
84 |
-
# Add user message to chat history
|
85 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
86 |
-
with st.chat_message("user"):
|
87 |
-
st.markdown(prompt)
|
88 |
-
|
89 |
-
# Generate response
|
90 |
-
with st.chat_message("assistant"):
|
91 |
-
with st.spinner("Thinking..."):
|
92 |
-
# Include scraped content in context if available
|
93 |
-
context = ""
|
94 |
-
if st.session_state.scrape_results:
|
95 |
-
context = f"Scraped information: {str(st.session_state.scrape_results)}\n"
|
96 |
-
|
97 |
-
full_prompt = f"{context}User: {prompt}\nAssistant:"
|
98 |
-
|
99 |
-
response = model(
|
100 |
-
full_prompt,
|
101 |
-
max_length=1000,
|
102 |
-
temperature=0.7,
|
103 |
-
top_p=0.95,
|
104 |
-
repetition_penalty=1.15
|
105 |
-
)[0]["generated_text"]
|
106 |
-
|
107 |
-
# Clean up response to get only the assistant's reply
|
108 |
-
response = response.split("Assistant:")[-1].strip()
|
109 |
-
|
110 |
-
st.markdown(response)
|
111 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
3 |
|
4 |
# Page config
|
5 |
st.set_page_config(
|
6 |
+
page_title="Zephyr Chat",
|
7 |
page_icon="π€",
|
8 |
layout="wide"
|
9 |
)
|
|
|
12 |
if "messages" not in st.session_state:
|
13 |
st.session_state.messages = []
|
14 |
|
15 |
+
# Load model and tokenizer
|
|
|
|
|
|
|
16 |
@st.cache_resource
|
17 |
def load_model():
|
18 |
+
model_name = "HuggingFaceH4/zephyr-7b-beta"
|
19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
20 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
21 |
+
return model, tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# Main chat interface
|
24 |
st.title("Zephyr Chatbot π€")
|
25 |
|
26 |
+
try:
|
27 |
+
model, tokenizer = load_model()
|
|
|
|
|
28 |
|
29 |
+
# Display chat messages
|
30 |
+
for message in st.session_state.messages:
|
31 |
+
with st.chat_message(message["role"]):
|
32 |
+
st.markdown(message["content"])
|
33 |
+
|
34 |
+
# Chat input
|
35 |
+
if prompt := st.chat_input("What's on your mind?"):
|
36 |
+
# Add user message to chat history
|
37 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
38 |
+
with st.chat_message("user"):
|
39 |
+
st.markdown(prompt)
|
40 |
+
|
41 |
+
# Generate response
|
42 |
+
with st.chat_message("assistant"):
|
43 |
+
with st.spinner("Thinking..."):
|
44 |
+
# Prepare input
|
45 |
+
input_text = f"User: {prompt}\nAssistant:"
|
46 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
47 |
+
|
48 |
+
# Generate response
|
49 |
+
outputs = model.generate(
|
50 |
+
inputs.input_ids,
|
51 |
+
max_length=200,
|
52 |
+
num_return_sequences=1,
|
53 |
+
temperature=0.7,
|
54 |
+
pad_token_id=tokenizer.eos_token_id
|
55 |
+
)
|
56 |
+
|
57 |
+
# Decode and display response
|
58 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
59 |
+
response = response.split("Assistant:")[-1].strip()
|
60 |
+
|
61 |
+
st.markdown(response)
|
62 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
63 |
+
|
64 |
+
except Exception as e:
|
65 |
+
st.error(f"Error: {str(e)}")
|
66 |
+
st.info("Note: This app requires significant computational resources. Consider using a smaller model or upgrading your Space's resources.")
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|