RadRuss commited on
Commit
22fc718
·
verified ·
1 Parent(s): 989b70d

Upload 3 files

Browse files
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. app.py +119 -67
  3. assets/logo.png +3 -0
  4. 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 huggingface_hub import InferenceClient
3
-
4
-
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
  """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
 
16
  """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
-
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- messages.extend(history)
22
-
23
- messages.append({"role": "user", "content": message})
24
-
25
- response = ""
26
-
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- type="messages",
49
- additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
- ],
61
- )
62
-
63
- with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
67
-
68
-
69
- if __name__ == "__main__":
70
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

  • SHA256: 9862591036faac13448a0812621e8378f14bbb0426e2b2fc664af227c8890b43
  • Pointer size: 131 Bytes
  • Size of remote file: 238 kB
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio==4.42.0
2
+ openai>=1.17.0
3
+ huggingface_hub