fyzanshaik commited on
Commit
a489e6a
·
verified ·
1 Parent(s): 5259b06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -47
app.py CHANGED
@@ -1,64 +1,82 @@
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- """
5
- 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
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
8
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
  ):
18
- messages = [{"role": "system", "content": system_message}]
 
 
 
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
 
27
 
28
- response = ""
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.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
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
  ],
 
 
 
 
 
 
 
60
  )
61
 
62
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
+ # app.py
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
 
5
+ # --- Configuration ---
6
+ # Use your friend's model directly via the Inference API
7
+ # The Inference API URL for a model is typically 'https://api-inference.huggingface.co/models/{model_id}'
8
+ # You can also pass just the model_id if it's on the public API:
9
+ CLIENT_MODEL_ID = "neuralnets/cf_codebot"
10
+ client = InferenceClient(CLIENT_MODEL_ID)
11
 
12
 
13
+ # --- Inference Function ---
14
+ # This function will call the Hugging Face Inference API
15
+ def generate_editorial_from_api(
16
+ problem_statement: str,
17
+ max_tokens: int, # Use max_new_tokens for the actual generation length
18
+ temperature: float,
19
+ top_p: float,
20
  ):
21
+ # The InferenceClient for text generation usually expects a direct string input
22
+ # and not necessarily the chat format (messages list) unless it's a specific chat model.
23
+ # For a text generation model like cf_codebot (which is GPT-2 based),
24
+ # you typically just send the input text.
25
 
26
+ # You might want to add a prompt structure here if your friend's model
27
+ # was fine-tuned with one, e.g., "Problem: {problem_statement}\nEditorial: "
28
+ prompt = problem_statement
29
+
30
+ # Call the Inference API for text generation
31
+ # The parameters might vary slightly depending on the specific model type
32
+ # but these are common for text generation.
33
+ # We use stream=False for now to get the full response at once for simplicity,
34
+ # as the model isn't designed for a chat interface, but rather a single generation.
35
+ try:
36
+ response = client.text_generation(
37
+ prompt=prompt,
38
+ max_new_tokens=max_tokens, # Renamed to max_new_tokens for clarity
39
+ temperature=temperature,
40
+ top_p=top_p,
41
+ do_sample=True, # Usually good for creative text generation
42
+ # Add stop_sequences if the model generates specific end tokens like "<end_of_turn>"
43
+ stop_sequences=["<end_of_turn>"] # Add this if your friend's model reliably uses it
44
+ )
45
+ # The response from text_generation is usually the generated string directly
46
+ # or a dictionary that needs parsing depending on client version.
47
+ # Let's assume it returns the string directly for now.
48
+ editorial_content = response.strip()
49
 
50
+ # If it still includes the problem statement, try to remove it (heuristic)
51
+ if editorial_content.startswith(problem_statement):
52
+ editorial_content = editorial_content[len(problem_statement):].strip()
53
+
54
+ return editorial_content
55
 
56
+ except Exception as e:
57
+ print(f"Error during API inference: {e}")
58
+ return f"An error occurred during editorial generation: {e}. Check logs for details."
59
 
 
 
 
 
 
 
 
 
60
 
61
+ # --- Gradio Interface Setup ---
62
+ # Adapted from your original generated chat interface
63
+ demo = gr.Interface(
64
+ fn=generate_editorial_from_api,
65
+ inputs=[
66
+ gr.Textbox(lines=10, label="Problem Statement", placeholder="Paste your problem statement here...", autofocus=True),
67
+ gr.Slider(minimum=1, maximum=1024, value=400, step=1, label="Max new tokens"), # Adjust max for editorials
 
 
 
 
 
68
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
69
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
70
  ],
71
+ outputs=gr.Textbox(label="Generated Editorial"),
72
+ title="Codeforces Editorial Assistant (via Hugging Face Inference API)",
73
+ description="Enter a problem statement to get a generated editorial from neuralnets/cf_codebot.",
74
+ allow_flagging="auto",
75
+ examples=[
76
+ ["A. Watermelon\ntime limit per test\n1 second\nmemory limit per test\n64 megabytes\n\nOne hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem.\n\nPete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight.\nInput\n\nThe first (and the only) input line contains integer number w (1 ≤ w ≤ 100) — the weight of the watermelon bought by the boys.\nOutput\n\nPrint YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case."]
77
+ ]
78
  )
79
 
80
 
81
  if __name__ == "__main__":
82
+ demo.launch()