Spaces:
Sleeping
Sleeping
Regino
commited on
Commit
Β·
c83e41c
1
Parent(s):
e1b0f3d
sdnfkd
Browse files- app.py +139 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import speech_recognition as sr
|
3 |
+
from openai import OpenAI
|
4 |
+
|
5 |
+
# Initialize session state for chat visibility
|
6 |
+
if "chat_started" not in st.session_state:
|
7 |
+
st.session_state.chat_started = False
|
8 |
+
|
9 |
+
if "chat_history" not in st.session_state:
|
10 |
+
st.session_state.chat_history = []
|
11 |
+
if "feedback" not in st.session_state:
|
12 |
+
st.session_state.feedback = {}
|
13 |
+
|
14 |
+
st.set_page_config(page_title="π¬Chatbot", page_icon="π¬", layout="wide")
|
15 |
+
|
16 |
+
# π― Welcome Screen (Only Show if Chat Not Started)
|
17 |
+
if not st.session_state.chat_started:
|
18 |
+
st.title("π€ Welcome to Deepseek AI Chatbot!")
|
19 |
+
st.write("A smart chatbot powered by the ne model Deepseek R1, designed to assist you with text generation.")
|
20 |
+
|
21 |
+
st.subheader("β¨ Features")
|
22 |
+
st.markdown("""
|
23 |
+
- π **Generate content**: Stories, articles, code, poems, dialogues, and more!
|
24 |
+
- ποΈ **Voice Input**: Speak instead of typing your prompt.
|
25 |
+
- π **Customizable Tone & Format**: Choose between formal, informal, humorous, technical styles.
|
26 |
+
- βοΈ **Adjustable Creativity**: Control randomness with temperature settings.
|
27 |
+
- π **Chat History**: Review past conversations and feedback.
|
28 |
+
""")
|
29 |
+
|
30 |
+
if st.button("π Start Chat"):
|
31 |
+
st.session_state.chat_started = True
|
32 |
+
st.rerun()
|
33 |
+
st.stop()
|
34 |
+
|
35 |
+
st.title("π€ Deepseek AI Chatbot!")
|
36 |
+
|
37 |
+
# π€ Function to capture voice input
|
38 |
+
def get_voice_input():
|
39 |
+
recognizer = sr.Recognizer()
|
40 |
+
with sr.Microphone() as source:
|
41 |
+
st.info("π€ Listening... Speak now!")
|
42 |
+
try:
|
43 |
+
audio = recognizer.listen(source, timeout=5)
|
44 |
+
text = recognizer.recognize_google(audio)
|
45 |
+
return text
|
46 |
+
except sr.UnknownValueError:
|
47 |
+
st.error("π Could not understand the audio.")
|
48 |
+
except sr.RequestError:
|
49 |
+
st.error("π Speech Recognition service unavailable.")
|
50 |
+
return ""
|
51 |
+
|
52 |
+
# π€ Voice Input Button
|
53 |
+
if st.button("π€ Speak Prompt"):
|
54 |
+
voice_text = get_voice_input()
|
55 |
+
if voice_text:
|
56 |
+
st.session_state["user_prompt"] = voice_text
|
57 |
+
else:
|
58 |
+
st.warning("No voice input detected.")
|
59 |
+
|
60 |
+
# π User Input Text Area
|
61 |
+
user_prompt = st.text_area("Enter your prompt:", st.session_state.get("user_prompt", ""))
|
62 |
+
|
63 |
+
# βοΈ Sidebar Settings
|
64 |
+
st.sidebar.header("βοΈ Settings")
|
65 |
+
output_format = st.sidebar.selectbox("Select Output Format", ["Story", "Poem", "Article", "Code", "Dialogue"])
|
66 |
+
tone = st.sidebar.selectbox("Select Tone/Style", ["Formal", "Informal", "Humorous", "Technical"])
|
67 |
+
temperature = st.sidebar.slider("Creativity Level (Temperature)", 0.0, 1.0, 0.7)
|
68 |
+
max_tokens = st.sidebar.slider("Response Length (Max Tokens)", 100, 1024, 500)
|
69 |
+
creative_mode = st.sidebar.checkbox("Enable Creative Mode", value=True)
|
70 |
+
|
71 |
+
# π Chat History in Sidebar
|
72 |
+
st.sidebar.header("π Chat History")
|
73 |
+
if st.sidebar.button("ποΈ Clear Chat History"):
|
74 |
+
st.session_state.chat_history = []
|
75 |
+
st.session_state.feedback = {}
|
76 |
+
|
77 |
+
with st.sidebar.expander("π View Chat History", expanded=False):
|
78 |
+
for i, chat in enumerate(reversed(st.session_state.chat_history)):
|
79 |
+
st.markdown(f"**User:** {chat['user']}")
|
80 |
+
bot_preview = chat['bot'][:200] + ("..." if len(chat['bot']) > 200 else "")
|
81 |
+
st.markdown(f"**Bot:** {bot_preview}")
|
82 |
+
|
83 |
+
if len(chat['bot']) > 200:
|
84 |
+
if st.toggle(f"π Show Full Response ({i+1})", key=f"toggle_{i}"):
|
85 |
+
st.markdown(chat['bot'])
|
86 |
+
|
87 |
+
feedback_value = st.session_state.feedback.get(chat['user'], "No Feedback Given")
|
88 |
+
st.markdown(f"**Feedback:** {feedback_value}")
|
89 |
+
st.markdown("---")
|
90 |
+
|
91 |
+
response_container = st.empty()
|
92 |
+
feedback_container = st.empty()
|
93 |
+
|
94 |
+
# π Generate Response
|
95 |
+
if st.button("Generate Response"):
|
96 |
+
if user_prompt.strip():
|
97 |
+
client = OpenAI(
|
98 |
+
base_url="https://integrate.api.nvidia.com/v1",
|
99 |
+
api_key="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"
|
100 |
+
)
|
101 |
+
|
102 |
+
messages = [{"role": "user", "content": f"Generate a {output_format.lower()} in {tone.lower()} style: {user_prompt}"}]
|
103 |
+
|
104 |
+
try:
|
105 |
+
completion = client.chat.completions.create(
|
106 |
+
model="tiiuae/falcon3-7b-instruct",
|
107 |
+
messages=messages,
|
108 |
+
temperature=temperature,
|
109 |
+
top_p=0.9 if creative_mode else 0.7,
|
110 |
+
max_tokens=max_tokens,
|
111 |
+
stream=True
|
112 |
+
)
|
113 |
+
|
114 |
+
response_text = ""
|
115 |
+
progress_text = st.empty()
|
116 |
+
|
117 |
+
for chunk in completion:
|
118 |
+
if chunk.choices[0].delta.content is not None:
|
119 |
+
response_text += chunk.choices[0].delta.content
|
120 |
+
progress_text.markdown(f"### Generating... β³\n{response_text}")
|
121 |
+
|
122 |
+
progress_text.empty()
|
123 |
+
response_container.markdown(f"### Generated Response\n{response_text}")
|
124 |
+
|
125 |
+
chat_entry = {"user": user_prompt, "bot": response_text}
|
126 |
+
st.session_state.chat_history.append(chat_entry)
|
127 |
+
|
128 |
+
feedback = feedback_container.radio(
|
129 |
+
"Was this response helpful?",
|
130 |
+
["π Yes", "π No"],
|
131 |
+
index=None,
|
132 |
+
key=f"feedback_{len(st.session_state.chat_history)}",
|
133 |
+
horizontal=True
|
134 |
+
)
|
135 |
+
|
136 |
+
st.session_state.feedback[user_prompt] = feedback
|
137 |
+
|
138 |
+
except Exception as e:
|
139 |
+
st.error(f"β Error: {e}")
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
openai
|
3 |
+
speechrecognition
|
4 |
+
pydub
|