akhaliq HF Staff commited on
Commit
284c90a
·
verified ·
1 Parent(s): 22bc6d0

Deploy from anycoder

Browse files
Files changed (2) hide show
  1. app.py +40 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+
4
+ # Initialize the client with a lightweight, capable model
5
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
+
7
+ def respond(message, history):
8
+ """
9
+ Formats the chat history and streams the response from the LLM.
10
+ """
11
+ messages = [{"role": "system", "content": "You are a helpful assistant."}]
12
+
13
+ # Convert Gradio history to OpenAI format
14
+ for val in history:
15
+ if val[0]: messages.append({"role": "user", "content": val[0]})
16
+ if val[1]: messages.append({"role": "assistant", "content": val[1]})
17
+
18
+ messages.append({"role": "user", "content": message})
19
+
20
+ # Stream response
21
+ response = ""
22
+ for msg in client.chat_completion(messages, max_tokens=512, stream=True, temperature=0.7):
23
+ token = msg.choices[0].delta.content
24
+ if token:
25
+ response += token
26
+ yield response
27
+
28
+ # Build the Interface
29
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
30
+ gr.Markdown("### [Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)")
31
+
32
+ gr.ChatInterface(
33
+ respond,
34
+ title="50-Line Chatbot",
35
+ description="A streaming AI assistant built with Gradio and Hugging Face Inference API.",
36
+ examples=["Tell me a joke", "Write a python script", "What is the capital of France?"]
37
+ )
38
+
39
+ if __name__ == "__main__":
40
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio>=4.0.0