NassimeBejaia commited on
Commit
ee57fa7
·
verified ·
1 Parent(s): c3351c9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import os
4
+
5
+ # Get DeepSeek API key from Space secrets
6
+ DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
7
+ DEEPSEEK_API_URL = "https://api.deepseek.com/v1/completions"
8
+ HEADERS = {"Authorization": f"Bearer {DEEPSEEK_API_KEY}", "Content-Type": "application/json"}
9
+
10
+ # Initialize session state
11
+ if "chat_history" not in st.session_state:
12
+ st.session_state.chat_history = []
13
+ if "corrected_sentence" not in st.session_state:
14
+ st.session_state.corrected_sentence = ""
15
+
16
+ # Title of the app
17
+ st.title("Sentence Improver & Chat with DeepSeek")
18
+
19
+ # --- Sentence Correction Section ---
20
+ st.subheader("Improve a Sentence")
21
+ user_input = st.text_input("Enter a sentence to improve:", "I goed to the park and play.")
22
+
23
+ if st.button("Improve Sentence"):
24
+ if user_input:
25
+ prompt = f"Correct and improve this sentence: '{user_input}'"
26
+ payload = {
27
+ "model": "deepseek-coder", # Adjust if needed
28
+ "prompt": prompt,
29
+ "max_tokens": 100,
30
+ "temperature": 0.7
31
+ }
32
+ try:
33
+ response = requests.post(DEEPSEEK_API_URL, headers=HEADERS, json=payload)
34
+ response.raise_for_status()
35
+ st.session_state.corrected_sentence = response.json()["choices"][0]["text"].strip()
36
+ st.success(f"Improved Sentence: {st.session_state.corrected_sentence}")
37
+ except Exception as e:
38
+ st.error(f"Error: {str(e)}")
39
+ else:
40
+ st.warning("Please enter a sentence first!")
41
+
42
+ # --- Chat Section ---
43
+ st.subheader("Chat About the Corrected Sentence")
44
+ if st.session_state.corrected_sentence:
45
+ # Chat history container with scrollbar
46
+ chat_container = st.container(height=300)
47
+ with chat_container:
48
+ for speaker, message in st.session_state.chat_history:
49
+ if speaker == "You":
50
+ st.markdown(
51
+ f"<div style='text-align: right; margin: 5px;'><span style='background-color: #DCF8C6; padding: 8px; border-radius: 10px;'>{message}</span></div>",
52
+ unsafe_allow_html=True
53
+ )
54
+ else:
55
+ st.markdown(
56
+ f"<div style='text-align: left; margin: 5px;'><span style='background-color: #ECECEC; padding: 8px; border-radius: 10px;'>{message}</span></div>",
57
+ unsafe_allow_html=True
58
+ )
59
+
60
+ # Chat input with Enter submission
61
+ chat_input = st.text_input(
62
+ "Ask something about the corrected sentence (press Enter to send) ➡️",
63
+ key="chat_input",
64
+ value="",
65
+ on_change=lambda: submit_chat(),
66
+ )
67
+
68
+ # Function to handle chat submission with full context
69
+ def submit_chat():
70
+ chat_text = st.session_state.chat_input
71
+ if chat_text:
72
+ # Build prompt with corrected sentence and full chat history
73
+ prompt = f"The corrected sentence is: '{st.session_state.corrected_sentence}'.\n"
74
+ if st.session_state.chat_history:
75
+ prompt += "Previous conversation:\n"
76
+ for speaker, message in st.session_state.chat_history:
77
+ prompt += f"{speaker}: {message}\n"
78
+ prompt += f"User asks: '{chat_text}'. Respond naturally."
79
+
80
+ payload = {
81
+ "model": "deepseek-coder",
82
+ "prompt": prompt,
83
+ "max_tokens": 150,
84
+ "temperature": 0.7
85
+ }
86
+ try:
87
+ response = requests.post(DEEPSEEK_API_URL, headers=HEADERS, json=payload)
88
+ response.raise_for_status()
89
+ llm_response = response.json()["choices"][0]["text"].strip()
90
+ # Add to chat history
91
+ st.session_state.chat_history.append(("You", chat_text))
92
+ st.session_state.chat_history.append(("LLM", llm_response))
93
+ # Clear input
94
+ st.session_state.chat_input = ""
95
+ except Exception as e:
96
+ st.error(f"Error in chat: {str(e)}")
97
+
98
+ else:
99
+ st.write("Improve a sentence first to start chatting!")
100
+
101
+ # Optional: Clear chat button
102
+ if st.button("Clear Chat"):
103
+ st.session_state.chat_history = []
104
+ st.rerun()