LilTomat0/TextCorrection
0
1import gradio as gr2from huggingface_hub import InferenceClient3 4"""5For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference6"""7client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")8 9 10 11instruction_prompt = (12 "Please correct the following text for the list of common writing mistakes in assignments and theses: "13 "1. Jargon and technical terms: Using specialized language that the audience may not understand, making the writing difficult to follow and possibly alienating readers. "14 "2. Inappropriate tone: Writing in a tone that is too formal or informal, which can be off-putting for the audience."15 "3. Overgeneralization: Making sweeping statements without providing sufficient evidence or support. "16 "4. Lack of transitions: Failing to connect ideas and paragraphs smoothly, resulting in disjointed and difficult-to-follow writing. "17 "5. Passive voice: Using passive constructions excessively, making the writing less engaging and harder to understand."18 "6. Lack of concrete examples: Failing to provide real-world examples or anecdotes to help illustrate abstract concepts. "19 "7. Inadequate or unclear introductions and conclusions: Failing to provide a clear roadmap for the reader, making it difficult to understand the main points and purpose of the writing. "20 "8. Inaccurate or incomplete citations: Failing to properly credit sources or provide complete citation information, leading to plagiarism or an appearance of intellectual dishonesty. "21 "9. Wordiness: Using more words than necessary to convey a point, making the writing less concise and harder to read. "22 "10. Assuming reader knowledge: Writing as if the reader already knows the topic, leading to insufficient description of key concepts and potential confusion. "23 "11. Awkward and difficult-to-follow sentences: Crafting sentences that are hard to read or understand, often due to poor syntax or grammar. "24 "12. Poor use of punctuation: Misusing commas, semicolons, and other punctuation marks, resulting in unclear or confusing sentences. "25 "13. Long, complex sentences: Writing sentences that are overly long and convoluted, making them difficult for readers to follow. "26 "14. Circular logic: Presenting arguments that assume the conclusion is true, rather than providing adequate evidence to support the claim. "27 "15. Irrelevant details from research: Including unnecessary or unrelated information from research, which can distract from the main points and weaken the overall argument. "28 "16. Disorganized paragraphs: Failing to structure paragraphs logically, making it difficult for readers to follow the flow of ideas. "29 "17. Inconsistent tense: Shifting between past, present, and future tense within a text, which can be confusing for the reader. "30 "18. Incorrect word choice: Using words that are inappropriate or do not accurately convey the intended meaning. "31 "19. Redundancy: Repeating information or ideas unnecessarily, which can make the writing less concise and engaging. "32 "20. Misuse of homophones: Confusing words that sound alike but have different meanings, such as 'their,' 'there,' and 'they're.' "33 "21. Ambiguity: Writing in a way that is open to multiple interpretations or lacks clarity, making it difficult for the reader to understand the intended meaning. "34 "22. Lack of balance: Providing too much detail or focus on one aspect of the topic while neglecting others, which can result in an unbalanced or biased presentation of information. "35 "23. Plagiarism: Using someone else's words, ideas, or work without proper attribution, which is a serious academic and ethical violation. "36 "Here is the text:"37 38)39 40# Define the response function with fixed parameters41def respond(42 message,43 history: list[tuple[str, str]],44):45 # Fixed parameters46 system_message = "You are a critical and helpful professor, providing writing feedback to your students."47 max_tokens = 51248 temperature = 0.549 top_p = 0.8550 51 # Combine the instruction prompt with the user's message52 full_message = instruction_prompt + " " + message53 54 # Create message history55 messages = [{"role": "system", "content": system_message}]56 for val in history:57 if val[0]:58 messages.append({"role": "user", "content": val[0]})59 if val[1]:60 messages.append({"role": "assistant", "content": val[1]})61 62 # Add the full_message to messages as the user's input63 messages.append({"role": "user", "content": full_message})64 65 # Get response66 response = ""67 for message in client.chat_completion(68 messages,69 max_tokens=max_tokens,70 stream=True,71 temperature=temperature,72 top_p=top_p,73 ):74 token = message.choices[0].delta.content75 response += token76 yield response77 78# Define Gradio ChatInterface without additional inputs79demo = gr.ChatInterface(80 respond81)82 83if __name__ == "__main__":84 demo.launch()85 