Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- .gitattributes +1 -0
- app.py +119 -67
- assets/logo.png +3 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
assets/logo.png filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
|
@@ -1,70 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
"""
|
| 15 |
-
|
|
|
|
| 16 |
"""
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
# Import dotenv to load environment variables from a .env file
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
# Load environment variables from a .env file
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# -----------------------------
|
| 11 |
+
# Configuration
|
| 12 |
+
# -----------------------------
|
| 13 |
+
# Use os.getenv to load environment variables
|
| 14 |
+
OPENAI_API_KEY = os.getenv("OPENAI_MOCEAN_KEY")
|
| 15 |
+
PROMPT_ID = os.getenv("PROMPT_ID")
|
| 16 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 17 |
+
|
| 18 |
+
from huggingface_hub import login
|
| 19 |
+
import os
|
| 20 |
+
|
| 21 |
+
if "HF_TOKEN" in os.environ:
|
| 22 |
+
login(os.environ["HF_TOKEN"])
|
| 23 |
+
|
| 24 |
import gradio as gr
|
| 25 |
+
from openai import OpenAI
|
| 26 |
+
import os
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# Server-side memory
|
| 31 |
+
conversation_state = {}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def generate_response(chat_id, message):
|
|
|
|
|
|
|
| 35 |
"""
|
| 36 |
+
Handles bot response using streaming,
|
| 37 |
+
maintains server-side conversation state.
|
| 38 |
"""
|
| 39 |
+
|
| 40 |
+
if chat_id not in conversation_state:
|
| 41 |
+
conversation_state[chat_id] = []
|
| 42 |
+
|
| 43 |
+
history = conversation_state[chat_id]
|
| 44 |
+
|
| 45 |
+
# Add user message to state
|
| 46 |
+
history.append({"role": "user", "content": message})
|
| 47 |
+
|
| 48 |
+
# Build input list
|
| 49 |
+
input_messages = history.copy()
|
| 50 |
+
|
| 51 |
+
# Create streaming response
|
| 52 |
+
stream = client.responses.stream(
|
| 53 |
+
model="gpt-4.1-mini",
|
| 54 |
+
input=input_messages,
|
| 55 |
+
prompt={"prompt_id": PROMPT_ID}
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
full_reply = ""
|
| 59 |
+
|
| 60 |
+
for event in stream:
|
| 61 |
+
if event.type == "response.output_text.delta":
|
| 62 |
+
token = event.delta
|
| 63 |
+
full_reply += token
|
| 64 |
+
yield full_reply # Stream token-by-token
|
| 65 |
+
|
| 66 |
+
# Save bot response to history
|
| 67 |
+
conversation_state[chat_id].append(
|
| 68 |
+
{"role": "assistant", "content": full_reply}
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def start_chat():
|
| 73 |
+
"""Creates a unique chat session ID."""
|
| 74 |
+
import uuid
|
| 75 |
+
return str(uuid.uuid4())
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# ---------- GRADIO UI --------------
|
| 79 |
+
|
| 80 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 81 |
+
gr.HTML("""
|
| 82 |
+
<div style='display:flex; align-items:center; gap:15px;'>
|
| 83 |
+
<img src='assets/logo.png' style='height:60px;'>
|
| 84 |
+
<h2 style='margin:0;'>Company Research Assistant</h2>
|
| 85 |
+
</div>
|
| 86 |
+
""")
|
| 87 |
+
|
| 88 |
+
chat_id = gr.State()
|
| 89 |
+
chatbot = gr.Chatbot(height=450)
|
| 90 |
+
|
| 91 |
+
msg = gr.Textbox(
|
| 92 |
+
placeholder="Ask me anything…",
|
| 93 |
+
label="Your Message"
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
def user_send(message, chat_history, cid):
|
| 97 |
+
if not cid:
|
| 98 |
+
cid = start_chat()
|
| 99 |
+
chat_history.append((message, None)) # Temporary blank
|
| 100 |
+
return "", chat_history, cid
|
| 101 |
+
|
| 102 |
+
def bot_reply(chat_history, cid):
|
| 103 |
+
user_msg = chat_history[-1][0]
|
| 104 |
+
response_stream = generate_response(cid, user_msg)
|
| 105 |
+
final_reply = ""
|
| 106 |
+
|
| 107 |
+
for chunk in response_stream:
|
| 108 |
+
final_reply = chunk
|
| 109 |
+
chat_history[-1] = (user_msg, final_reply)
|
| 110 |
+
yield chat_history
|
| 111 |
+
|
| 112 |
+
msg.submit(
|
| 113 |
+
user_send,
|
| 114 |
+
inputs=[msg, chatbot, chat_id],
|
| 115 |
+
outputs=[msg, chatbot, chat_id]
|
| 116 |
+
).then(
|
| 117 |
+
bot_reply,
|
| 118 |
+
inputs=[chatbot, chat_id],
|
| 119 |
+
outputs=chatbot
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
demo.launch()
|
assets/logo.png
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.42.0
|
| 2 |
+
openai>=1.17.0
|
| 3 |
+
huggingface_hub
|