richardkimsm89 commited on
Commit
e82a10b
·
verified ·
1 Parent(s): da3d905

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -2
app.py CHANGED
@@ -15,7 +15,7 @@ app = gr.load(
15
  ]
16
  ).launch()
17
  """
18
-
19
  # Pipeline
20
 
21
  import gradio as gr
@@ -39,4 +39,57 @@ app = gr.Interface(
39
  examples = [
40
  ["Hello, World."]
41
  ]
42
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  ]
16
  ).launch()
17
  """
18
+ """
19
  # Pipeline
20
 
21
  import gradio as gr
 
39
  examples = [
40
  ["Hello, World."]
41
  ]
42
+ ).launch()
43
+ """
44
+
45
+ import gradio as gr
46
+ from huggingface_hub import InferenceClient
47
+ import os
48
+
49
+ hf_token = os.getenv("HF_TOKEN")
50
+ client = InferenceClient(api_key=hf_token)
51
+
52
+ def chatbot(input_text, history):
53
+ # Prepare the conversation messages
54
+ messages = [{"role": "user", "content": input_text}]
55
+
56
+ # Add conversation history (if exists)
57
+ if history:
58
+ for user_input, bot_response in history:
59
+ messages.append({"role": "user", "content": user_input})
60
+ messages.append({"role": "assistant", "content": bot_response})
61
+
62
+ # Generate model response
63
+ stream = client.chat.completions.create(
64
+ model="google/gemma-2-2b-it",
65
+ messages=messages,
66
+ temperature=0.5,
67
+ max_tokens=2048,
68
+ top_p=0.7,
69
+ stream=True
70
+ )
71
+
72
+ # Collect response from the model
73
+ bot_response = ""
74
+ for chunk in stream:
75
+ bot_response += chunk.choices[0].delta.content
76
+
77
+ # Update the conversation history
78
+ history.append((input_text, bot_response))
79
+ return bot_response, history
80
+
81
+ # Create Gradio Interface
82
+ with gr.Blocks() as demo:
83
+ chatbot_ui = gr.Chatbot(label="Gemma Chatbot").style(height=400)
84
+ text_input = gr.Textbox(show_label=False, placeholder="Type your message here...")
85
+ state = gr.State([]) # Keeps track of conversation history
86
+
87
+ def user_input_handler(user_input, chatbot_ui, state):
88
+ bot_response, state = chatbot(user_input, state)
89
+ chatbot_ui.append((user_input, bot_response))
90
+ return chatbot_ui, state
91
+
92
+ text_input.submit(user_input_handler, [text_input, chatbot_ui, state], [chatbot_ui, state])
93
+
94
+ # Run the application
95
+ demo.launch()